Simple use of Aidl
1 aidl (Android Interface definition Language): interface Definition language.
2 When you need to pass objects between different processes, such as passing objects between different applications, you need to use Aidl.
3 aid enables an application to invoke services from another application.
First, the server application (in fact, a service application) 1 Create a Aidl file
The development tools used here are Android Studio, create the Aidl folder under the main folder, and the Java folder sibling, create the package in the Aidl folder, and the package name must match the application package name. Finally create the Aidl file: Peopleserviceaidl.aidl
package com.miquan.demo;// AIDL其实就是一个接口,定义了通讯的规则。interface PeopleServiceAIDL { String findPeople(int num);}
Click Build > Make Module xxx in the Android Studio menu, and a corresponding class will be generated. Specific location: Build > Generated > Source > Aidl > Debug > XXX
2 Creating a Service
Peopleservice.java
PackageCom.miquan.demo;ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException; Public class peopleservice extends Service { PrivateIBinder Mbinder =NewPeopleservicebinder (); Public Peopleservice() { }@Override PublicIBinderOnbind(Intent Intent) {returnMbinder; }Private Final class peopleservicebinder extends peopleserviceaidl. Stub { @Override PublicStringFindpeople(intNumthrowsremoteexception {returnnum = =0?"Wang Nima":"Tangmaru"; } }}
Configure the service in the manifest file (with an implicit start):
<service android:name=".PeopleService"> <intent-filter> <action android:name="com.miquan.demo.people.service"/> </intent-filter></service>
At this point the server is finished.
II. client (caller side)
The first thing to do is to create the Aidl file in the same way, or copy it to correspond.
PackageCOM.MIQUAN.AS3;Importandroid.app.Activity;ImportAndroid.content.ComponentName;ImportAndroid.content.Intent;ImportAndroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportCom.miquan.demo.PeopleServiceAIDL; Public class mainactivity extends Activity { PrivateButton Mbutton;PrivatePeopleserviceaidl Mpeopleserviceaidl;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Mbutton = (Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {Try{//The name obtained here is obtained from a service in another application.String name = Mpeopleserviceaidl.findpeople (0); }Catch(RemoteException e) {E.printstacktrace (); } } }); Bindservice (NewIntent ("Com.miquan.demo.people.service"), Mserviceconnection, bind_auto_create); }PrivateServiceconnection mserviceconnection =NewServiceconnection () {@Override Public void onserviceconnected(componentname name, IBinder service) {mpeopleserviceaidl = PeopleServiceAIDL.Stub.asInterface (service); }@Override Public void onservicedisconnected(componentname name) {Mpeopleserviceaidl =NULL; } };@Override protected void OnDestroy() {Super. OnDestroy (); Unbindservice (mserviceconnection); }}
Simple use of Aidl