In Android, each application has its own process, and how does it work when you need to pass objects between different processes? It is obvious that there is no support for sharing across process memory in Java. So to pass the object, we need to parse the object into the data format that the operating system can understand to achieve the goal of Cross-border object access. In Java EE, RMI is used to pass objects through serialization. In Android, it is implemented using the Aidl (Android Interface definitionlanguage: Interface Description Language) approach.
Aidl is an interface definition language that is used to constrain the communication rules between two processes for the compiler to generate code to implement two Interprocess communications (IPC) on the Android device. The IPC mechanism of AIDL is similar to that of CORBA used in EJB, the communication information between processes is first converted into AIDL protocol message, and then sent to the other side, the other party receives the AIDL protocol message and converts it to the corresponding object. Since the communication between processes requires bidirectional conversion, Android uses proxy classes to implement bidirectional information conversion, and the proxy class is generated by the Android compiler, which is transparent to the developer.
Service side:
Calculateinterface.aidl package com.itheima.aidl.calculate; Interface Calculateinterface {Double docalculate (double A, double b);}//calculateservice.java Package Com.itheima.myai
Dl.server;
Import Com.itheima.aidl.calculate.CalculateInterface;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
Import android.os.RemoteException;
Import Android.util.Log; public class Calculateservice extends service{private final calculateinterface.stub mbinder = new Calculateinterface.stu
B () {@Override public double docalculate (double A, double b) throws remoteexception {return a+b;}}; @Override public IBinder onbind (Intent Intent) {log.i ("test", "Onbind ..."); return mbinder;} @Override public Boolean OnU Nbind (Intent Intent) {log.i ("test", "Onunbind ..."); return Super.onunbind (Intent);} @Override public void OnCreate () {su
Per.oncreate ();
LOG.I ("Test", "onCreate ...");
@Override public void OnDestroy () {Super.ondestroy ();
LOG.I ("Test", "OnDestroy ...");
}
}Server-side manifast file <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= " Com.itheima.myaidl.server "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android: minsdkversion= "android:targetsdkversion="/> <application android:allowbackup= "true" android:icon= "@ Drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity android: Name= "com.itheima.myaidl.server.MainActivity" android:configchanges= "locale|layoutdirection" android:theme= "@ Android:style/theme.light.notitlebar "android:screenorientation=" Portrait "> <intent-filter> <action
Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name= "Com.itheima.myaidl.server.CalculateService" > <intent-filter> <action android:name= "Com.itheima.myaidl.server.CalculateService"/> </ Intent-filter> ≪/service> </application> </manifest>//client//mainactivity.java package com.itheima.myaidl.client;
Import android.app.Activity;
Import Android.content.ComponentName;
Import Android.content.Context;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import android.os.RemoteException;
Import Android.util.Log;
Import Com.itheima.aidl.calculate.CalculateInterface; public class Mainactivity extends activity {private calculateinterface mservice; private Serviceconnection Mserviceconne ction = new Serviceconnection () {@Override public void onservicedisconnected (componentname name) {log.i ("test", "service
Disconnected ... ");
Mservice = null; @Override public void onserviceconnected (componentname name, IBinder service) {log.i ("test", "Service connected ..."); m Service = CalculateInterface.Stub.asInterface (service);
Get interface Instance}}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreatE (savedinstancestate);
Setcontentview (R.layout.activity_main);
Bind remote Service Bundle Bundle = new Bundle ();
Intent Intent = new Intent ();
Intent.putextras (bundle);
Intent.setaction ("Com.itheima.myaidl.server.CalculateService");
Bindservice (Intent, mserviceconnection, context.bind_auto_create); Callback this method when//todo activity finishes loading @Override public void Onwindowfocuschanged (Boolean hasfocus) {if (hasfocus) {try{double Resul
t = mservice.docalculate (1, 2);
LOG.I ("Test", "result===>" +result);
}catch (RemoteException e) {e.printstacktrace ();}}
Super.onwindowfocuschanged (Hasfocus); @Override protected void OnDestroy () {unbindservice (mserviceconnection);//unbound remote Service Super.ondestroy ();}}
Run Results screenshot:
The above is a small set of Android to introduce how to use the AIDL mechanism to invoke remote service related knowledge, hope to help everyone!