Android's understanding Aidl

Source: Internet
Author: User

1. What is Aidl? Android Interface Definition lauguage (Android Interface Description Language) is an IDL language.    2, the role of Aidl? Background: In the Android platform, a process typically does not have access to memory areas in other processes. So, they need to split the objects into simple forms that the operating system can understand so that they are disguised as objects across the boundary. Writing this kind of camouflage code is quite tedious, but Android gives us the Aidl tool to do it: for interprocess communication, there are many people who might ask, there are many ways of interprocess communication, why do we have to use Aidl? There are indeed many inter-process communication, such as broadcast, Message,content Provider. These are inter-process communication, but broadcasts are unidirectional, and message can only communicate in the same process, not across processes, and Content provider is not real-time.  And Aidl has the advantages that they do not have, bidirectional and cross-process communication, real-time.  So, under what circumstances did you use Aidl? Official document Description: Note:using Aidl is necessary only if you allow clients from different applications to access your service for IPC (inter-process communication, interprocess communication) I and want to handle multithreading (multithreaded) in your service. If you don't need to perform concurrent (concurrent) IPC across different applications, you should create your interface by Implem   Enting a Binder or, if you want to perform IPC, but does not need to handle multithreading, implement your interface using a Messenger.  Regardless, be sure this understand Bound Services before implementing an aidl. The approximate meaning of the translation is: Allow different customers to come fromWhen the application accesses your server and handles multithreading issues you must use Aidl 3, use the process to use Aidl,service to provide the service interface in Aidl file, Aidl tool will generate a corresponding Java interface, and a stub service pile class that contains a function call in the generated service interface. The implementation class of the service needs to inherit the stub services pile class. The Onbind method of the service returns the object that implements the class, and then you can use it. 4, applet application first create a suffix named Aidl file, my manager.aidl package com.test.service.aidl;  Interface manager{float Add (float num1,float num2);} Write a class that implements it Managerimpl.javapackage Com.test.service.aidl;public class Managerimpl extends manager.stub{@Override p    ublic float Add (float num1,float num2) {return num1 + num2; }} write a service class Myservice.javapackage Com.test;import Com.test.service.aidl.manager;import Com.test.service.aidl.managerimpl;import Android.app.service;import Android.content.intent;import        Android.os.ibinder;import Android.util.log;public class MyService extends service{private manager.stub binder;        public void OnCreate () {super.oncreate ();        Binder = new Managerimpl ();        SYSTEM.OUT.PRINTLN ("---->oncreate");    LOG.E ("--->", "---->oncreate");    }    public int Onstartcommand (Intent Intent, int flags, int startid) {System.out.println ("---->onstart");    Return Super.onstartcommand (Intent, flags, Startid);    }//Outside to access the service, the @Override public ibinder onbind (Intent arg0) {return binder is accessed through the returned binder; }} in Androidmainfest.xml configuration resource android:process= ":p Ush" is another process <service android:name= ".        MyService "android:enabled=" true "android:exported=" true "android:process=":p Ush "> </service> finally wrote the test class mainactivity.javapackage Com.test.ui.activity;import Com.test.service.aidl.Manager; Import Com.test.myservice;import com.test.r;import com.test.service.aidl.managerimpl;import android.os.Bundle; Import Android.os.ibinder;import Android.os.remoteexception;import Android.view.view;import Android.widget.edittext;import Android.widget.textview;import Android.app.activity;import Android.content.componentname;import Android.content.intent;import Android.content.SerViceconnection;public class Mainactivity extends Activity {private Manager binder;    Private EditText etnum1,etnum2;        Private TextView Tvresult;        Private Serviceconnection Serviceconnect = new Xmppserviceconnect ();        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        SYSTEM.OUT.PRINTLN ("---->oncreate1");    Init ();     } protected void OnStart () {Super.onstart (); When the activity starts binding myservice, it automatically calls Serviceconnect Bindservice (new Intent (this, myservice.class), Serviceconnect, BIND_AUTO_CR    Eate);        } private void Init () {etNum1 = (EditText) Findviewbyid (R.ID.ETNUM1);        etNum2 = (EditText) Findviewbyid (r.id.etnum2);    Tvresult = (TextView) Findviewbyid (R.id.tvresult);        } protected void OnDestroy () {Super.ondestroy ();    Unbindservice (Serviceconnect); } public void Btn_add_click (View v) throws remoteexception{float num2 = float.parsefloat (Etnum2.gettext (). toString ());        float NUM1 = float.parsefloat (Etnum1.gettext (). toString ());    Tvresult.settext (Binder.add (NUM1, num2) + ""); } Private class Xmppserviceconnect implements Serviceconnection {//Start binding service when you execute the following method public void Onservic Econnected (componentname componentname, IBinder ibinder) {binder = Manager.Stub.asInterface (IBinder);//Special attention here I Binder is the service that passed over the}//unbind when calling public void onservicedisconnected (ComponentName componentname) {b        Inder = null; }}} The interface is very simple, two edittext, a textview and a button en end, and if there's something wrong, you want someone to point it out.

  

Android's understanding Aidl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.