Android-cross-application binding service (AIDL)
In the previous chapter, we recorded the differences and usage of startService in bindService.
To use the bindService method, the activity requires the service to return a Binder object. How can we implement the Binder object in two applications? Here we will discuss the concept of 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.
Let's take a look at how to use it,
First, we need to create an aidl interface in the service1 project.
I am using eclipse, create method, right-click the package name-NEW-File-Name of the. aidl suffix **,
Enter the interface code in the aidl file.
After the file is created, the clear project will produce the corresponding java file under the gen directory.
Step 2
EnterModify onBinder Method for service in service1 Project <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPqOstPrC68jnz8I8L3A + DQo8cHJlIGNsYXNzPQ = "brush: java;"> @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new IMyService.Stub() { @Override public void basicType() throws RemoteException { // TODO Auto-generated method stub } }; }
So far, a simple aidl is successfully created.
Go back to the service2 project and add button event listening,
case R.id.button3: bindService(serviceIntent, this, Context.BIND_AUTO_CREATE); break; case R.id.button4: unbindService(this); break;
The service in service1 project is successfully bound to the service2 project.