AIDL is an IPC (Inter-Process Communication) method in Android. AIDL is the abbreviation of Android Interface definition language (For novices, the function of AIDL is to enable you to bind a service of other APPs in your APP, so that your APP can interact with other APPs.)
AIDL is just one of the many inter-process communication methods in Android.
The difference between AIDL and Messenger:
- Messenger does not apply to a large number of concurrent requests: Messenger processes messages sent by the client in a serial way. If a large number of messages are sent to the server at the same time, the server can still only process them one by one.
- Messenger is mainly used to deliver messages: for methods that require cross-process calls to servers, this scenario does not apply to Messenger.
- The underlying implementation of Messenger is AIDL, and the system encapsulates it for us to facilitate calls from the upper layer.
- AIDL is suitable for a large number of concurrent requests and cases involving server-side method calls.
The principle of AIDL communication: First, look at this file with a class called proxy, which is a proxy class. This class runs in the client. In fact, the communication between processes implemented by AIDL is not direct communication. Both the client and the server communicate through proxy: the method called by the client is actually called a method in proxy, and then the proxy returns the returned result to the client through communication with the server.
1. The role of AIDL
AIDL is used for Android's IPC communication, so it can communicate within one APP or create two APPs for communication.
The function allocation of AIDL is very clear. Service runs as a background and manages various interactions as a server, and Client as a client to request data or call Service methods.
2. Simple use of AIDL
1) Create an aidl file, just right-click to create it.
package ;
// package ; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); String add(int x , int y); }
2) Select the .aidl file you just created to produce the corresponding java file.
AndroidStudio can be completed through the Build--》model App
3) Write specific objects of the Service to implement interfaces
package ; import ; import ; import ; import ; import ; public class FirstService extends Service { public FirstService() { } private static String Tag = "FirstService"; @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. //throw new UnsupportedOperationException("Not yet implemented"); (Tag,"service on bind"); return mBinder; } @Override public void onCreate() { (); (Tag,"OnCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { (Tag,"onStartCommand"); return START_STICKY; } @Override public boolean onUnbind(Intent intent) { (Tag,"onUnbind"); return (intent); } @Override public void onDestroy() { (); (Tag,"onDestroy"); } mBinder = new (){ @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public String add(int x, int y) throws RemoteException { (Tag,x + "--" + y); return (x + y); } }; }
Note: onBund returns the IBinder type, and will be called for the subsequent callbacks.
4) Determine the Service content inside
<service android:name=".FirstService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name=""/> </intent-filter> </service>
5) Open the service
private Button btnStartService; private Button btnBindService; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); initView(); } private void initView() { tvId = (TextView) findViewById(.tv_id); btnStartService = (Button) findViewById(.btn_Start_Service); (new () { @Override public void onClick(View view) { Intent intent = new Intent(); (""); (""); startService(intent); } }); btnBindService = (Button) findViewById(); (new () { @Override public void onClick(View view) { bind(); } }); private void bind(){ (Tag, "bind"); Intent intent = new Intent(); (""); if(controllerConnection != null){ (intent,controllerConnection,this.BIND_AUTO_CREATE);//Bind the service and create a link } else { (Tag, "controllerConnection != null"); } } private void unbind(){ if(controllerConnection != null && ().isBinderAlive()){ try{ (Tag, "(controllerConnection);"); (controllerConnection); } catch (Exception localException) { (Tag, "unbind Exception localException"); } } }
It is asynchronous when binding, so you can use onServiceConnected() to determine the operation after binding.
private ServiceConnection controllerConnection = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { (Tag,"onServiceConnected"); myAIDLController = (iBinder); if (myAIDLController != null) { try { (Tag, "ServiceConnection success"); (, "ServiceConnection success", Toast.LENGTH_LONG).show(); } catch (Exception localException) { (Tag, "Exception localException"); } } } @Override public void onServiceDisconnected(ComponentName componentName) { (Tag,"onServiceDisconnected"); (, "onServiceDisconnected", Toast.LENGTH_LONG).show(); myAIDLController = null; } @Override public void onBindingDied(ComponentName name) { (Tag,"onBindingDied"); } @Override public void onNullBinding(ComponentName name) { (Tag,"onNullBinding"); } };
6) Call Service method add()
When calling, you need to bind the Service method. It already has it on it. Next, it is simple to call it. Create a Button, then get the Service's control object, and call the method add
btnServiceFunc = (Button) findViewById(); (new () { @Override public void onClick(View view) { try { (Tag, ( (1,2))); } catch (RemoteException e) { (); } } });
This is the end of this article about how to use Android aidl. For more information about Android aidl, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!