Android process communication: Aidl Getting Started instance

Source: Internet
Author: User

Aidl is the Android Interface Definition Language. Reason: On Android, one process cannot normally access thememory of another process.

That is to say, Aidl is used for Android interprocess communication, the first Aidl demo is recorded below.

The official documentation also provides basic methods of use, such as:


1. Create a aidl file under the SRC related package for Android project Iremoteservice.aidl:

Package Com.example.testndk;interface Iremoteservice {    /** Request the process ID of this service, to do evil things W ITH it. *    /int getpid ();    /** demonstrates some basic types that's can use as parameters     * and return values in Aidl.     *    /void basictypes (int anint, long along, Boolean aboolean, float afloat,            double adouble, String astring);}
Once created, a Java file with the same name will be automatically generated in the Gen directory, open to see that it is actually an interface

Public interface Iremoteservice extends Android.os.IInterface


2. Implement this interface

Package Com.example.testndk;import Android.app.service;import Android.content.intent;import android.os.IBinder; Import Android.os.process;public class Remoteservice extends Service {    @Override public    void OnCreate () {        Super.oncreate ();    }    @Override public    IBinder onbind (Intent Intent) {        //Return the interface        return mbinder;    }    Private final Iremoteservice.stub Mbinder = new Iremoteservice.stub () {public        int getpid () {            return Process.mypid ();        }        public void basictypes (int anint, long along, Boolean Aboolean,            float afloat, double adouble, String astring) {            Does Nothing        }}    ;}

3. Expose the interface to the client, that is, at the call

Package Com.example.testndk;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 Android.widget.textview;public class Testndkactivity extends Activity {private static final String TAG = "Testndkactiv    ity ";    Private TextView TV1 = null;    static {//System.loadlibrary ("TESTNDK"); }/** called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        String str = jniclient.addstr ("prefix", "suffix");        //int isum = Jniclient.addint (5, 2);        String Strsum = "5 + 2 =" + isum;        TV1 = new TextView (this); Bindservice (New Intent (RemoteService.class.getName ()), Mconnection, context.bind_auto_CREATE);    Setcontentview (TV1);    } Iremoteservice miremoteservice = null;  Private Serviceconnection mconnection = new Serviceconnection () {//called when the connection and the service is  Established public void onserviceconnected (componentname className, IBinder service) {//following the            Example above for a aidl interface,//This gets a instance of the Iremoteinterface, which we can use            to the service Miremoteservice = IRemoteService.Stub.asInterface (service);            try {tv1.settext ("miremoteservice=" + miremoteservice.getpid ());            } catch (RemoteException e) {//TODO auto-generated catch block E.printstacktrace (); }}//Called when the connection with the service disconnects unexpectedly public void Onservi cedisconnected (componentname className) {log.e (TAG, "Service has unexpectedly DisconnecTed ");        Miremoteservice = null;    }    };        protected void OnDestroy () {Super.ondestroy ();    Unbindservice (mconnection); }}

Use the service, also need to configure in Androidmanifest.xml, add
        <service android:name= "Com.example.testndk.RemoteService" >            <intent-filter>                <action Android:name= "Com.example.testndk.RemoteService"/>            </intent-filter>        </service>

Because the service is found through action, the action tag is not limited.

An example of an official document is a little more troublesome, but it is also quite clear.


Android process communication: Aidl Getting Started instance

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.