It is not possible to pass the binder directly.
1, first write a aidl file, which contains the method we want to communicate. (Android Studio has a direct new aidl option)
Interface MyInterface { /** * Demonstrates some basic types that's can use as parameters * and return values in Aidl. */ void basictypes (intlongbooleanfloat afloat, Double adouble, String astring); // SayHello string SayHello (string name);}
The compiler will help us generate the appropriate Java files, but now we can't see them in the AS.
The abstract inner class stub in the interface inherits the Android.os.Binder class and implements the MyInterface interface, and the more important method is the Asinterface (IBinder) method, This method converts an object of type IBinder to a myinterface type.
When using, we just need to define the interface, and then use the automatically generated stub class.
2. Implement Service
Public classMyServiceextendsservice{
Implementing the stub class is actually the previous custom binder myinterface.stub stub=Newmyinterface.stub () {@Override Public voidBasictypes (intAnint,LongAlong,BooleanAboolean,floatAfloat,DoubleAdouble, String astring)throwsRemoteException {}
Overriding the communication method for the Adil file @Override PublicString SayHello (string name)throwsRemoteException {//Toast.maketext (myservice.this, "hello" + "," +name,toast.length_short). Show ();
Toast can only be in the UI thread
return"Hello" + "," +name; } }; @Nullable @Override Publicibinder onbind (Intent Intent) {LOG.D ("TAG", "Onbind");
Returns a custom binderreturnstub; } @Override Public voidonCreate () {Super. OnCreate (); LOG.D ("TAG", "OnCreate"); } @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.D ("TAG", "Ondestory"); }}
Manifest.xml
<service android:name= ". MyService " android:process=": remote > <intent-filter> <action android:name= " Android.intent.action.MyService "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-filter> </service>
3. Activity
This is not the same as before. Instead of a binder, a reference to an interface converts the binder returned by the service to an instance of an interface. Again, you need to implement serviceconnection yourself.
Public classMainactivityextendsappcompatactivity {//this is an interface. PrivateMyInterface MyInterface; PrivateServiceconnection conn =Newserviceconnection () {@Override Public voidonserviceconnected (componentname componentname, IBinder ibinder) {//Instantiate a service stub (Binder) on our interfaceMyInterface =MyInterface.Stub.asInterface (IBinder); } @Override Public voidonservicedisconnected (ComponentName componentname) {}}; //two x buttonsButton Hello,bind; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Bind=(Button) Findviewbyid (R.id.bind); Hello=(Button) Findviewbyid (R.id.say); Bind.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {
Binding Intent Intent=NewIntent (mainactivity. This, MyService.class); StartService (Intent); Bindservice (intent,conn,bind_auto_create); } }); Hello.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Try {
Communication, the UI thread toast String temp= Myinterface.sayhello ("Xiao Ming"); Toast.maketext (mainactivity. This, Temp,toast.length_short). Show (); } Catch(RemoteException e) {e.printstacktrace (); } } }); }}
Without special instructions, service and activity I guess it's running in the same thread as the same process, and it's the UI thread. Because you can toast directly inside the service. After the remote service is defined, they run in a different process, and can write a sleep in the service to see if the app is stuck to verify. If you want to use a toast, you'll need to use Message Queuing to send messages to the UI thread to toast.
Remote Servie Communication Aidl