android之回调操作

 1.为什么要回调?因为一个对象A做完某个操作之后要把返回结果返回给另一个对象B,然后B再继续做其他的操作。通过接口对象C建立桥梁,A可以传递参数并调用这个接口对象C,B具体实现C的方法。A调用C的方法的时候就是调用的B里实现的C的方法。


2.

转自http://blog.csdn.net/bewhatyouare/article/details/8957454

Android之activity跳转并且回调

假设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接口。

[java] view plaincopy
  1. public static class FragmentA extends ListFragment {  
  2.     ...  
  3.     // Container Activity must implement this interface  
  4.     public interface OnArticleSelectedListener {  
  5.         public void onArticleSelected(Uri articleUri);  
  6.     }  
  7.     ...  
  8. }  

然后,持有这个Fragment的Activity要实现OnArticleSelectedListener接口,并且要重写onArticleSelected()方法把来自Fragment A的事件通知给Fragment B。要确保持有Fragment的Activity实现这个接口, Fragment A 的onAttach()回调方法(当Fragment被添加到Activity时系统调用这个方法)通过类型转换onAttach()传入的Activity来实例化一个OnArticleSelectedListener的实例。

[java] view plaincopy
  1. public static class FragmentA extends ListFragment {  
  2.     OnArticleSelectedListener mListener;  
  3.     ...  
  4.     @Override  
  5.     public void onAttach(Activity activity) {  
  6.         super.onAttach(activity);  
  7.         try {  
  8.             mListener = (OnArticleSelectedListener) activity;  
  9.         } catch (ClassCastException e) {  
  10.             throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");  
  11.         }  
  12.     }  
  13.     ...  
  14. }  

如果这个Activity没有实现这个接口,那么Fragment会抛出ClassCastException异常。如果成功,那么mListener成员就会拥有Activity实现的OnArticleSelectedListener对象的引用,以便Fragment A能够通过OnArticleSelectedListener接口定义的回调方法和Activity共享事件。例如,如果ListFragment继承了Fragment A,那么用户每次点击列表项时,系统都会调用Fragment中的onListItemClick()方法,然后调用onArticleSelected()方法和Activity共享事件:

[java] view plaincopy
  1. public static class FragmentA extends ListFragment {  
  2.     OnArticleSelectedListener mListener;  
  3.     ...  
  4.     @Override  
  5.     public void onListItemClick(ListView l, View v, int position, long id) {  
  6.         // Append the clicked item's row ID with the content provider Uri  
  7.         Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);  
  8.         // Send the event and Uri to the host activity  
  9.         mListener.onArticleSelected(noteUri);  
  10.     }  
  11.     ...  
  12. }  

传递给onListItemClick()的id参数是被点击项目的行ID,Activity(或其他的Fragment)使用这个ID从应用程序的ContentProvider对象中获取对应的文章。

关于使用有效内容提供器的更多内容,请看内容提供器(Content Providers)文档。

注:本人转载系个人觉得翻译的很好,值得收藏,且自己回头看着方便。

如有兴趣请访问作者官方博客http://blog.csdn.net/FireOfStar

4.

转自

 Android中aidl实现Activity与service进行通信和回调 

service一直再运行,通过bindService拿到service的代理,并将自己到回调对象注册过去,就能实现调用service中的方法,和在service中调用本地activity到方法。做到了进程间通信。

ImyserviceManager.aidl
  1. package com.test;

  2. import com.test.Ilisten;

  3. interface ImyserviceManager
  4. {
  5.     int add(int a,int b);
  6.     String show();
  7.     void register(Ilisten listen);
  8. }

RemoteService.java

  1. package com.test;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.os.RemoteException;
  6. import android.util.Log;

  7. public class RemoteService extends Service
  8. {
  9.     Ilisten myListener = null;
  10.     public class ServiceImpl extends ImyserviceManager.Stub
  11.     {
  12.         public int add(int a,int b)throws RemoteException
  13.         {
  14.             if(myListener != null)
  15.                 myListener.change("this is call back!");
  16.             return (a+b);
  17.         }
  18.         
  19.         public String show()throws RemoteException
  20.         {
  21.             return "hello world!";
  22.         }

  23.         public void register(Ilisten listen) throws RemoteException 
  24.         {
  25.             // TODO Auto-generated method stub
  26.             myListener = listen;
  27.         }
  28.     }
  29.     


  30.     @Override
  31.     public IBinder onBind(Intent intent) 
  32.     {
  33.         // TODO Auto-generated method stub
  34.         return new ServiceImpl();
  35.     }

  36.     @Override
  37.     public int onStartCommand(Intent intent, int flags, int startId) {
  38.         // TODO Auto-generated method stub
  39.         Log.i("test","I am running .......................");
  40.         return super.onStartCommand(intent, flags, startId);
  41.         
  42.     }
  43.     
  44.     
  45. }

Ilisten.aidl

  1. package com.test;

  2. interface Ilisten
  3. {
  4.     void change(String a);
  5. }

TestAidl.java
  1. package com.test;

  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.os.RemoteException;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.TextView;

  15. public class TestAidl extends Activity 
  16. {
  17.     String str = null;
  18.     private ImyserviceManager myManager;
  19.     Button myButton;
  20.     private TextView textView;
  21.     private Button button1;
  22.     private Button button2;
  23.     
  24.     private ServiceConnection serviceConnection =new ServiceConnection()
  25.     {

  26.         public void onServiceConnected(ComponentName name, IBinder service) 
  27.         {
  28.             // TODO Auto-generated method stub+
  29.             
  30.             myManager=ImyserviceManager.Stub.asInterface(service);
  31.             try {
  32.                 Log.i("test-------",myManager.show());
  33.                 TextView textView=(TextView)findViewById(R.id.text);
  34.                 textView.setText(str);
  35.                 myManager.register(new myListener());
  36.                 
  37.             } catch (RemoteException e) {
  38.                 // TODO Auto-generated catch block
  39.                 e.printStackTrace();
  40.             }
  41.         }

  42.         public void onServiceDisconnected(ComponentName name) 
  43.         {
  44.             // TODO Auto-generated method stub
  45.             
  46.         }
  47.         
  48.     };
  49.     
  50.     public class myListener extends Ilisten.Stub
  51.     {

  52.         public void change(String a) throws RemoteException 
  53.         {
  54.             // TODO Auto-generated method stub
  55.             
  56.             button1.setText(a);
  57.             
  58.         }
  59.         
  60.     }
  61.     
  62.     /** Called when the activity is first created. */
  63.     @Override
  64.     public void onCreate(Bundle savedInstanceState) 
  65.     {
  66.         super.onCreate(savedInstanceState);
  67.         setContentView(R.layout.main);
  68.         bindService(new Intent(TestAidl.this, RemoteService.class), serviceConnection, Context.BIND_AUTO_CREATE);
  69.   
  70.          textView=(TextView)findViewById(R.id.text);
  71.         
  72.          button1 = (Button) findViewById(R.id.b1);
  73.          
  74.          button1.setOnClickListener(new View.OnClickListener() {
  75.             
  76.             public void onClick(View v) 
  77.             {
  78.                 try {
  79.                     button1.setText(myManager.show());
  80.                     //myManager.add(1, 2);
  81.                 } catch (RemoteException e) {
  82.                     // TODO Auto-generated catch block
  83.                     e.printStackTrace();
  84.                 }
  85.             }
  86.         });
  87.          
  88.          button2= (Button)findViewById(R.id.b2);
  89.          button2.setOnClickListener(new View.OnClickListener() {
  90.             
  91.             public void onClick(View v) 
  92.             {
  93.                 try {
  94.                     myManager.add(2, 3);
  95.                 } catch (RemoteException e) {
  96.                     // TODO Auto-generated catch block
  97.                     e.printStackTrace();
  98.                 }
  99.             }
  100.         });
  101.      

  102.     }
  103. }
5.通过广播机制传递消息回调

A注册广播,B通过发送广播给A来实现A的回调。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章