1.为什么要回调?因为一个对象A做完某个操作之后要把返回结果返回给另一个对象B,然后B再继续做其他的操作。通过接口对象C建立桥梁,A可以传递参数并调用这个接口对象C,B具体实现C的方法。A调用C的方法的时候就是调用的B里实现的C的方法。
2.
转自http://blog.csdn.net/bewhatyouare/article/details/8957454
假设A页面要跳到B页面,A页面需要获取B页面传回来的参数来确定显示哪个列表。主要代码如下:
在A页面中:
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivityForResult(intent,REQUESTCODE);//REQUESTCODE定义一个整型做为请求对象标识
跳转到B,在B页面中:
B.setResult(parama, getIntent());//定义返回的参数parama
B.finish();
返回到A,在A页面中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//resultCode就是在B页面中返回时传的parama,可以根据需求做相应的处理
}
3.
Android Fragment---给Activity创建事件回调
转自http://blog.csdn.net/huaxin803/article/details/7506696
在某些案例中,可能需要Fragment与Activity共享事件。在Fragment内部定义一个回调接口是一个好方法,并且规定由持有它的Activity实现这个回调方法。当Activity通过接口接受回调时,它能在必要时与布局中的其他Fragment共享信息。
例如,如果一个新闻类的应用程序在一个Activity中有两个Fragment---一个用来显示文章列表(Fragment A),另一个用来显示文章内容(Fragment B)---然后再列表项目被选中时Fragment A必须告诉Activity,以便它能告诉Fragment B显示对应的文章。在下面的例子中在Fragment A的内部声明了OnArticleSelectedListener接口。
- public static class FragmentA extends ListFragment {
- ...
- // Container Activity must implement this interface
- public interface OnArticleSelectedListener {
- public void onArticleSelected(Uri articleUri);
- }
- ...
- }
然后,持有这个Fragment的Activity要实现OnArticleSelectedListener接口,并且要重写onArticleSelected()方法把来自Fragment A的事件通知给Fragment B。要确保持有Fragment的Activity实现这个接口, Fragment A 的onAttach()回调方法(当Fragment被添加到Activity时系统调用这个方法)通过类型转换onAttach()传入的Activity来实例化一个OnArticleSelectedListener的实例。
- public static class FragmentA extends ListFragment {
- OnArticleSelectedListener mListener;
- ...
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- mListener = (OnArticleSelectedListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
- }
- }
- ...
- }
如果这个Activity没有实现这个接口,那么Fragment会抛出ClassCastException异常。如果成功,那么mListener成员就会拥有Activity实现的OnArticleSelectedListener对象的引用,以便Fragment A能够通过OnArticleSelectedListener接口定义的回调方法和Activity共享事件。例如,如果ListFragment继承了Fragment A,那么用户每次点击列表项时,系统都会调用Fragment中的onListItemClick()方法,然后调用onArticleSelected()方法和Activity共享事件:
- public static class FragmentA extends ListFragment {
- OnArticleSelectedListener mListener;
- ...
- @Override
- public void onListItemClick(ListView l, View v, int position, long id) {
- // Append the clicked item's row ID with the content provider Uri
- Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
- // Send the event and Uri to the host activity
- mListener.onArticleSelected(noteUri);
- }
- ...
- }
传递给onListItemClick()的id参数是被点击项目的行ID,Activity(或其他的Fragment)使用这个ID从应用程序的ContentProvider对象中获取对应的文章。
关于使用有效内容提供器的更多内容,请看内容提供器(Content Providers)文档。
注:本人转载系个人觉得翻译的很好,值得收藏,且自己回头看着方便。
如有兴趣请访问作者官方博客http://blog.csdn.net/FireOfStar
4.转自
Android中aidl实现Activity与service进行通信和回调
-
package com.test;
-
-
import com.test.Ilisten;
-
-
interface ImyserviceManager
-
{
-
int add(int a,int b);
-
String show();
-
void register(Ilisten listen);
- }
-
package com.test;
-
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import android.util.Log;
-
-
public class RemoteService extends Service
-
{
-
Ilisten myListener = null;
-
public class ServiceImpl extends ImyserviceManager.Stub
-
{
-
public int add(int a,int b)throws RemoteException
-
{
-
if(myListener != null)
-
myListener.change("this is call back!");
-
return (a+b);
-
}
-
-
public String show()throws RemoteException
-
{
-
return "hello world!";
-
}
-
-
public void register(Ilisten listen) throws RemoteException
-
{
-
// TODO Auto-generated method stub
-
myListener = listen;
-
}
-
}
-
-
-
-
@Override
-
public IBinder onBind(Intent intent)
-
{
-
// TODO Auto-generated method stub
-
return new ServiceImpl();
-
}
-
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
// TODO Auto-generated method stub
-
Log.i("test","I
am running .......................");
-
return super.onStartCommand(intent, flags, startId);
-
-
}
-
-
- }
-
package com.test;
-
-
interface Ilisten
-
{
-
void change(String a);
- }
-
package com.test;
-
-
import android.app.Activity;
-
import android.content.ComponentName;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.ServiceConnection;
-
import android.os.Bundle;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.TextView;
-
-
public class TestAidl extends Activity
-
{
-
String str = null;
-
private ImyserviceManager myManager;
-
Button myButton;
-
private TextView textView;
-
private Button button1;
-
private Button button2;
-
-
private ServiceConnection serviceConnection =new ServiceConnection()
-
{
-
-
public void onServiceConnected(ComponentName name, IBinder
service)
-
{
-
// TODO Auto-generated method stub+
-
-
myManager=ImyserviceManager.Stub.asInterface(service);
-
try {
-
Log.i("test-------",myManager.show());
-
TextView textView=(TextView)findViewById(R.id.text);
-
textView.setText(str);
-
myManager.register(new myListener());
-
-
} catch (RemoteException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
public void onServiceDisconnected(ComponentName name)
-
{
-
// TODO Auto-generated method stub
-
-
}
-
-
};
-
-
public class myListener extends Ilisten.Stub
-
{
-
-
public void change(String a) throws RemoteException
-
{
-
// TODO Auto-generated method stub
-
-
button1.setText(a);
-
-
}
-
-
}
-
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
bindService(new Intent(TestAidl.this, RemoteService.class), serviceConnection, Context.BIND_AUTO_CREATE);
-
-
textView=(TextView)findViewById(R.id.text);
-
-
button1 = (Button) findViewById(R.id.b1);
-
-
button1.setOnClickListener(new View.OnClickListener() {
-
-
public void onClick(View v)
-
{
-
try {
-
button1.setText(myManager.show());
-
//myManager.add(1, 2);
-
} catch (RemoteException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
});
-
-
button2= (Button)findViewById(R.id.b2);
-
button2.setOnClickListener(new View.OnClickListener() {
-
-
public void onClick(View v)
-
{
-
try {
-
myManager.add(2, 3);
-
} catch (RemoteException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
});
-
-
-
}
- }
A注册广播,B通过发送广播给A来实现A的回调。