Android Discovery Aidl for interprocess communication

Source: Internet
Author: User

Objective:

The previous summary of sharing data between programs, you can use ContentProvider can also use sharedpreference, then how to share memory between processes? There is no sharing of memory between processes in the Android system, so there are mechanisms that need to be provided for data communication between different processes.

To enable other applications to access the services provided by the application, the Android system is implemented using remote procedure calls (Procedure Call,rpc). Like many other RPC-based solutions, Android uses an interface definition language (Interface definition Language,idl) to expose the interfaces of the service. We know that 3 of the 4 Android application components (Activity, Broadcastreceiver, and ContentProvider) can be accessed across processes, and another Android application component service is also available. As a result, this service that can be accessed across processes is referred to as the Aidl (Android Interface Definition Language) service.

Let's do the actual combat. Specific implementation: 1.) first create a new Aidl file
Interface itestinterface {     // get process ID    int  getprocessid ();     // Working with Strings     string dealstring (string srcstring);     // string Append     string appendString (string srcstring);     void Addperson (in person person);    List<Person> getpersons ();}

Aidl Grammar Commentary:

    • The declaration function is basically consistent with Java, and can be passed parameters and return values, arguments and return values
    • Parameters and return values the basic data types of the Java programming language (int, long, char, Boolean, etc.), string and Charsequence, collection interface types list and map, other Aidl interface types, Implementing a custom object for the Parcelable interface
    • Direction indicates that, for non-basic data types, not string and charsequence types, (that is, parcelable types) require directional indications, including in, out, and inout when transmitting data using AIDL.
    • Aidl only supports interface methods and cannot expose static variables.
2.) Server-Side Implementation interface
    Private FinalItestinterface.stub Mbinder =Newitestinterface.stub () { Public intGetProcessID () {LOG.E ("Testservice", "Testservice Thread:" +Thread.CurrentThread (). GetName ()); LOG.E ("Testservice", "Testservice GetProcessID ()"); returnAndroid.os.Process.myPid (); }        //Working with Strings         Publicstring dealstring (String srcstring) {returnsrcstring+srcstring; }        //string Append         Publicstring appendString (String srcstring) {returnsrcstring+srcstring; }
3.) Client Get interface
   PrivateServiceconnection connection =Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {LOG.E ("Testservice", "Testservice onservicedisconnected ()"); } @Override Public voidonserviceconnected (componentname name, IBinder service) {Itestinterface=ITestInterface.Stub.asInterface (service); Try{LOG.E ("Testservice", "Testservice onserviceconnected ()"); intRemoteid=Itestinterface.getprocessid (); LOG.E ("Testservice", "Testservice remoteid---->" +Remoteid); intCurrentpid =Android.os.Process.myPid (); LOG.E ("Testservice", "Testservice currentpid---->" +currentpid); LOG.E ("Testservice", "Testservice dealstring---->" +itestinterface.dealstring ("Remote Service")); LOG.E ("Testservice", "Testservice appendString---->" +itestinterface.appendstring ("Remote Service")); } Catch(RemoteException e) {e.printstacktrace (); }        }    };
4.) call/pass data via IPC
                int remoteid=itestinterface.getprocessid ();                LOG.E ("Testservice", "Testservice  remoteid---->" +remoteid);                 int currentpid = android.os.Process.myPid ();                LOG.E ("Testservice", "Testservice  currentpid---->" +currentpid);                LOG.E ("Testservice", "Testservice  dealstring---->" +itestinterface.dealstring ("Remote Service"  ));                LOG.E ("Testservice", "Testservice  appendString---->" +itestinterface.appendstring ("Remote Service") );
5.) Service Declaration and binding/Unbinding

Statement:

       <ServiceAndroid:name=". Testservice "android:enabled= "true"android:exported= "true"Android:label= "Remoteservice"android:process= ": Remote">            <Intent-filterandroid:priority= "+">                <categoryAndroid:name= "Android.intent.category.DEFAULT" />                <ActionAndroid:name= "Com.whoislcj.testaidl.TestService" />            </Intent-filter>        </Service>

Binding:

                New Intent ("Com.whoislcj.testaidl.TestService");                Intent.setpackage (Getpackagename ()); // Here you need to set the package name                of your app Bindservice (Intent, connection, context.bind_auto_create);

Unbind:

               Unbindservice (connection);
6.) Access rights are consistent with service

Android Discovery Aidl for interprocess communication

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.