- BackGround
When I learned how to invoke one object's callback function, I found that the callback has three category:
First: in the local, who define one function in file, then give it to you, you get it and use it.
Second: You can use the intent to past one object to the others, then the others get the object and use it's method. This is called You define, I use.
Third: Use binder, binder can be pasted with different processes. And in binder, you can define method, then past the binder to others, then others get the binder and use its methods. - Sample Code//the class of being past object
1 package com.example.ddd; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 //must implements Parcelable and could not implements other interface. Not like zhangsan implements what, Parcelable 6 public class zhangsan implements Parcelable{ 7 8 public String getName() { 9 // TODO Auto-generated method stub 10 return "zhangsan"; 11 } 12 13 public void setName() { 14 // TODO Auto-generated method stub 15 16 } 17 18 @Override 19 public int describeContents() { 20 // TODO Auto-generated method stub 21 return 0; 22 } 23 24 @Override 25 public void writeToParcel(Parcel dest, int flags) { 26 // TODO Auto-generated method stub 27 28 } 29 public static final Parcelable.Creator<zhangsan> CREATOR = new Creator<zhangsan>() { 30 public zhangsan createFromParcel(Parcel source) { 31 zhangsan mBook= new zhangsan(); 32 return mBook; 33 } 34 public zhangsan[] newArray(int size) { 35 return new zhangsan[size]; 36 } 37 }; 38 39 }
//create the BroadcastReceiver
1 package com.example.binderservice; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.util.Log; 7 8 import com.example.ddd.zhangsan; 9 10 public class MyReceiver extends BroadcastReceiver{ 11 12 @Override 13 public void onReceive(Context context, Intent intent) { 14 // TODO Auto-generated method stub 15 Log.e("receiver","in receiver"); 16 zhangsan zhang =(zhangsan)intent.getParcelableExtra("object");//"object is a string which map to past object" 17 Log.e("receiver",zhang.getName());19 } 20 21 }
//setBroadcast
1 package com.example.interfacep; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.view.Menu; 7 8 import com.example.ddd.zhangsan; 9 10 public class MainActivity extends Activity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 zhangsan zhang = new zhangsan(); 17 Intent i = new Intent("intent");//"intent" is the intent.action name, in the receiver project AndroidManifest.xml,receiver <intent-filter> <action android:name="intent" /> 18 Bundle extras = new Bundle(); 19 extras.putParcelable("object", zhang);//"object is a string which map to the past object" 20 i.putExtras(extras); 21 sendBroadcast(i); 22 } 23 24 @Override 25 public boolean onCreateOptionsMenu(Menu menu) { 26 // Inflate the menu; this adds items to the action bar if it is present. 27 getMenuInflater().inflate(R.menu.activity_main, menu); 28 return true; 29 } 30 31 }