Use of Android process-to-process communication AIDL:
1. Introduction
In Android, threads cannot transmit information to each other and share their memory space. Therefore, Android provides AIDL.
AIDL (Android Interface Definition Language) is an IDL Language used to generate code for interprocess communication (IPC) between two processes on the Android device. If you want to call operations on objects in another process (such as Service) in a process (such as Activity), you can use AIDL to generate serializable parameters.
For the specific syntax and usage of AIDL, see http://developer.android.com/guide/components/aidl.html.
2. Use
The following is a simple example to illustrate the use of AIDL, which has no practical use. In this example, we provide two projects, server and client, which run in their own processes to implement the interactive communication process between them. Of course, click the button on the client to return the welcome message "hello aidl! ", The details are as follows:
Server:
On the server side, we provide the AIDL interface and Service, and bind the Service with AIDL Venus to listen for requests in real time.
AIDHelloService. aidl:
Interface AIDLHelloService {
String sayHelloAidl ();
}
RemoteService. java:
Public class RemoteService extends Service {
Private final AIDLHelloService. Stub binder = new AIDLHelloService. Stub (){
@ Override
Public String sayHelloAidl () throws RemoteException {
Return "hello aidl! ";
}
};
@ Override
Public IBinder onBind (Intent arg0 ){
Return binder;
}
}
Of course, this Service returns the welcome message "hello aidl!" when the client clicks the request !" To the client. After aidl is edited and saved, the. java file corresponding to aidl is automatically generated in the gen directory.
Client:
In the client, we only need to provide the request Click Event and the ServiceConnection that is responsible for connecting different processes, and use it to communicate with other processes. Because the client communicates with the server using the same AIDL, we only need to copy the aidl package implemented by the server to the SRC directory of the client, the following is the specific implementation code.
Client request click event:
Public class AIDLHelloClientActivity extends Activity {
Private AIDLHelloService aidlService;
Private static boolean isServiceBound = false;
Private ServiceConnection conn = new ServiceConnection (){
@ Override
Public void onServiceConnected (ComponentName name, IBinder service ){
Try {
AidlService = AIDLHelloService. Stub. asInterface (service );
} Catch (Exception e ){
E. printStackTrace ();
Return;
}
}
@ Override
Public void onServiceDisconnected (ComponentName name ){
AidlService = null;
}
};
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_aidlhello_client );
If (isServiceBound ){
UnbindService (conn );
} Else {
Intent intent = new Intent ("com. demo. aidlhelloserver. service. RemoteService ");
BindService (intent, conn, Context. BIND_AUTO_CREATE );
}
IsServiceBound =! IsServiceBound;
}
Public void sayHelloAidl (View v ){
If (null! = AidlService ){
Try {
Toast. makeText (this, aidlService. sayHelloAidl (),
Toast. LENGTH_SHORT). show ();
} Catch (RemoteException e ){
E. printStackTrace ();
Return;
}
}
}
Below is the running:
Server:
Client:
Now we have implemented the AIDL usage introduction.
3. Note
A. Remember to register the Service on the server
AndroidManifest. xml:
Android: name = ". service. RemoteService">
B. Copy AIDL from the server to the client.
C. If the. java file corresponding to. aidl is not generated under gen, recompile the project clean.
/*
* Android technology exchange group no.: 179914858
* Welcome to join
*/