Android services are designed to perform a number of operations, such as long-running time-consuming operations, more time-consuming network operations, or even a never-ending operation in a single process. One implementation of these operations is done through the Android Interface Definition language (AIDL). Aidl is designed to perform interprocess communication, and another way to achieve this is to see the messenger of communication between the Android processes in the blog post. In this article we will learn how to create a Aidl file for Android interprocess communication. Before we formally study, we should clarify some "facts".
About Android Service
1 , Android services are not background tasks, they are only run in the main thread of your app by default 2 , Android services can be set to run in different processes 3. If the Android service starts in a different process, you will not be able to communicate with the usual IBinder interface
Aidl:android Interface Definition Language
To enable interprocess communication in Android applications, we need to define a series of methods that can be accessed by the current process in the remote process. By Aidl We can define such a series of methods. Aidl is like other interfaces in Java where you can define some abstract methods. We first need to create a file with a suffix of. Aidl and define the required abstract methods inside.
One of the main features of Aidl is that by using aidl, we can communicate in two different applications (in fact Messenger can do the same thing, see the Messenger of communication between the Android processes in the blog); If your app doesn't need to communicate with another app, try to avoid using the aidl mechanism. In the abstract method defined in the Aidl file, only some primitive data types and some basic data types such as string,lists,maps can be used as parameters to these methods, and if you want to use a custom class as a parameter, your custom class must implement the Parcelable interface. And the class is to be imported into the Aidl file, which should be explained in a separate course. This article only learns the simple aidl usage.
AIDL Realization Idea
Aidl by defining the interface exposed by the server to provide to the client to invoke, Aidl enables the server side to be processed in parallel (so you may need to consider thread security issues for multithreaded concurrent access). By writing the Aidl file to design the interface that you want to expose, the compiler automatically generates the corresponding Java file, the server writes the specific implementation of the interface to the stub, passes the IBinder object to the client, and when the client Bindservice, The Asinterface method is used to restore the IBinder to an interface for the client to invoke the method.
Simple example
Androidmanifest.xml
<Application Android:allowbackup="true"Android:icon="@drawable/ic_launcher"Android:label="@string/app_name"Android:theme="@style/apptheme"> <Activity Android:name=". Mainactivity"Android:label="@string/app_name"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name=". Boundservice"android:process=": Custom_process"/></application>
Iboundservice.aidl
Package Yf.exam.service.aidl; Interface iboundservice{ int getresult (intint b);}
Boundservice.java
public Class Boundservice extends Service { Private iboundservice.stub Mbinder = new Iboundservice.stub () {@Override public int getresult (int A, int b) throws RemoteException { return a+B; } }; @Override public IBinder Onbind (Intent Intent) { return Mbinder; }}
Mainactivity.java
Public classMainactivity extends Activity {PrivateButton BTN =NULL; PrivateIboundservice Miboundservice; PrivateBoolean mserviceconnected =false; PrivateServiceconnection conn =Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {mserviceconnected=false; } @Override Public voidonserviceconnected (componentname name, IBinder service) {Miboundservice=IBoundService.Stub.asInterface (service); Mserviceconnected=true; } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.id.button1); Intent Intent=NewIntent ( This, Boundservice.class); Bindservice (Intent, Conn, context.bind_auto_create); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(mserviceconnected) {Try { intresult = Miboundservice.getresult (2,4); Toast.maketext (mainactivity. This,"result="+result, Toast.length_short). Show (); } Catch(RemoteException e) {e.printstacktrace (); } } } }); } @Overrideprotected voidOnDestroy () {Super.ondestroy (); if(mserviceconnected) {unbindservice (conn); Mserviceconnected=false; } }}
The aidl of communication between Android processes