http://blog.csdn.net/saintswordsman/article/details/5130947
Welcome to this article, you can follow this article, you know you need interprocess communication, need aidl (and binder), then you can default to these concepts have some understanding, you (roughly) know what they are, what is the use, so in order to save everyone's eyesight and time, Here I do not copy paste online flooding blog or translate lengthy Android documents.
Introduction to Aidl in Documentation: docs/guide/developing/tools/aidl.html
Introduction to IBinder in Documentation: docs/reference/android/os/ibinder.html
and binder:docs/reference/android/os/binder.html
In the following article, I will introduce you to the relevant concepts in my own understanding. In my current superficial experience, the application uses Aidl, and almost all of the service-related, so you also need to know some knowledge about the service. Later cannot I will continue to write some of the service stickers.
This article will use an example to share with you the basic skills of using aidl, in this case:
1, a class maidlactivity, inherit activity. There are three buttons, the text is startservice,stopservice,callbacktest.
2, a class Maidlservice, inherit service. To fully demonstrate the functionality of Adil, it does the following: When the user taps the Callbacktest button, a method Invokcallback () is called from maidlactivity to the Stub object in Maidlservice, This method also invokes a method performaction () of the Stub object in Maidlactivity, which displays a toast on the screen. It doesn't make sense to just show how aidl is used.
3, two aidl files: Forservice.aidl and Foractivity.aidl. corresponding to the name, each object in the service and activity needs to use the interface they define.
4, the relevant XML file, skip. For the syntax of service in manifest, see docs/guide/topics/manifest/service-element.html. You can also simply add to the <application></application>
<service android:name= ". Maidlservice" android:process= ": Remote" > </service>
The development environment is eclipse.
Pick the important first, take a look at the contents of the Aidl file:
Files: foractivity.aidl
Package Com.styleflying.AIDL; Interface foractivity { void performaction (); }
Files: forservice.aidl
Package Com.styleflying.AIDL; Import com.styleflying.AIDL.forActivity; Interface Forservice { void Registertestcall (foractivity CB); void Invokcallback (); }
These two files are the same as where the Java files are placed, looking at the package name.
In eclipse they will be automatically compiled into Foractivity.java and Forservice.java, which are stored in the Gen directory. In order to facilitate the reader at hand cannot drill, the code does not stick, do not look closely.
See Maidlactivity.java again:
PackageCom.styleflying.AIDL; Importandroid.app.Activity; ImportAndroid.content.ComponentName; ImportAndroid.content.Context; Importandroid.content.Intent; Importandroid.content.ServiceConnection; ImportAndroid.os.Bundle; ImportAndroid.os.IBinder; Importandroid.os.RemoteException; ImportAndroid.util.Log; ImportAndroid.view.View; ImportAndroid.view.View.OnClickListener; ImportAndroid.widget.Button; ImportAndroid.widget.Toast; Public classMaidlactivityextendsActivity {Private Static FinalString TAG = "Aidlactivity"; PrivateButton Btnok; PrivateButton Btncancel; PrivateButton Btncallback; Private voidLog (String str) {LOG.D (TAG,"------" + str + "------"); } PrivateForactivity Mcallback =Newforactivity.stub () { Public voidPerformaction ()throwsremoteexception {toast.maketext (maidlactivity. This, "This toast was called from service", 1). Show (); } }; Forservice Mservice; PrivateServiceconnection mconnection =Newserviceconnection () { Public voidonserviceconnected (componentname className, IBinder service) {Mservice=forService.Stub.asInterface (service); Try{mservice.registertestcall (mcallback);} Catch(RemoteException e) {}} Public voidonservicedisconnected (componentname className) {Log ("Disconnect Service"); Mservice=NULL; } }; @Override Public voidonCreate (Bundle icicle) {Super. OnCreate (Icicle); Setcontentview (R.layout.main); Btnok=(Button) Findviewbyid (R.ID.BTN_OK); Btncancel=(Button) Findviewbyid (r.id.btn_cancel); Btncallback=(Button) Findviewbyid (r.id.btn_callback); Btnok.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {Bundle args=NewBundle (); Intent Intent=NewIntent (maidlactivity. This, Maidlservice.class); Intent.putextras (args); Bindservice (Intent, mconnection, context.bind_auto_create); StartService (Intent); } }); Btncancel.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {unbindservice (mconnection); //StopService (Intent); } }); Btncallback.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Try{mservice.invokcallback (); } Catch(RemoteException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }); } }
Very short, I believe you can easily see. Note Mconnection, which has a sentence Mservice = forService.Stub.asInterface (service) in onserviceconnected (), and assigns a value to Mservice, This mservice is a forservice, and the service is onserviceconnected () passed in parameters, onserviceconnected () will be connected to the service when the system is called, Where does the value of this service parameter come from?
See Maidlservice.java:
PackageCom.styleflying.AIDL; ImportAndroid.app.Service; Importandroid.content.Intent; ImportAndroid.os.IBinder; Importandroid.os.RemoteCallbackList; Importandroid.os.RemoteException; ImportAndroid.util.Log; Public classMaidlserviceextendsService {Private Static FinalString TAG = "Aidlservice"; PrivateForactivity callback; Private voidLog (String str) {LOG.D (TAG,"------" + str + "------"); } @Override Public voidonCreate () {Log ("Service Create"); } @Override Public voidOnStart (Intent Intent,intStartid) {Log ("Service Start id=" +Startid); } @Override PublicIBinder onbind (Intent t) {Log ("Service on Bind"); returnMbinder; } @Override Public voidOnDestroy () {Log ("Service on Destroy"); Super. OnDestroy (); } @Override Public BooleanOnunbind (Intent Intent) {Log ("Service on Unbind"); return Super. Onunbind (Intent); } Public voidOnrebind (Intent Intent) {Log ("Service on Rebind"); Super. Onrebind (Intent); } Private FinalForservice.stub Mbinder =Newforservice.stub () {@Override Public voidInvokcallback ()throwsremoteexception {callback.performaction (); } @Override Public voidRegistertestcall (foractivity CB)throwsRemoteException {Callback=CB; } }; }
Note Onbind (), whose return type is IBinder, returns a mbinder to see the definition of Mbinder:
Private final Forservice.stub Mbinder = new Forservice.stub () {
@Override
public void Invokcallback () throws RemoteException
{
Callback.performaction ();
}
@Override
public void Registertestcall (Foractivity cb) throws RemoteException
{
callback = CB;
}
};
It implements the method we defined in Aidl, which eventually returns to Mservice in Maidlactivity, so the Maidlactivity method can be used in Mbinder. There is also an object similar to Mbinder in maidlactivity to see the definition:
Private foractivity Mcallback = new Foractivity.stub ()
{
public void Performaction () throws RemoteException
{
Toast.maketext (Maidlactivity.this, "This toast was called from service", 1). Show ();
}
};
We're going to show a toast on the interface, and that's where it's implemented. This object, when Mconnection onserviceconnected () is called, by calling Mbinder () of Mservice (that is, registertestcall in remote Maidlservice), Passed to Maidlservice, so you can call Performaction () in Maidlservice.
Very verbose, just to be able to make this detail clear. Please look seriously, I try to avoid typos, confusion of the size and logic of the grammar, I believe you will see. Isn't it simple? Again, do a rough summary of what we are going to do with Aidl:
Let Acticity (or a process/a class?) and service (or remote process/remote class/object?) Gets a stub object that implements the method we defined in Aidl when defined, so that the methods in these remote objects can be used locally. If this use (communication) is one-way, for example, only the activity needs to inform the service what to do, then as long as the service has a stub object, and passed to Acticity is enough.
As for how to get a remote stub, refer to the code above, see Mconnection, Registertestcall, Onrebind, and they show a way.
In addition, sometimes we may have multiple stub objects in a class, all of which have to give an instance of a class that interacts remotely, and this time consider using remotecallbacklist<> (docs/reference/android/os/ remotecallbacklist.html).
Android interprocess communication: Using Aidl