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的回調。


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