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