Aidl instance for Android service learning

Source: Internet
Author: User
Because each application runs in its own process space and can run another service process from the application UI, objects are often transferred between different processes. On the Android platform, a process generally cannot access the memory space of another process. Therefore, to perform a conversation, you need to break down the object into basic units that the operating system can understand and orderly pass through the process boundary.


The following describes how to implement aidl calling. This article includes aidl interface calling, callback, and transmission of complex data types.


Run demo


Aidl File
Person. aidl

Package com. archermind. aidl;

Parcelable person;

Itaskbinder. aidl

Package com. archermind. aidl;

Import com. archermind. aidl. itaskcallback;

Import com. archermind. aidl. person;

Interface itaskbinder {


Void fuc01 ();

Void fuc02 ();

String fuc03 (in person );

Void registercallback (itaskcallback CB );

Void unregistercallback ();

}

Itaskcallback. aidl

Package com. archermind. aidl;


Interface itaskcallback {

Void onactionback (string Str );

}


First look at the myservice code

The server implements the aidl interface for the client to call.

@ Override

Public ibinder onbind (intent arg0 ){

// Todo auto-generated method stub

Return mbinder;

}



Private Final itaskbinder. Stub mbinder = new itaskbinder. Stub (){



@ Override

Public void unregistercallback () throws RemoteException {

// Todo auto-generated method stub

Log. V (TAG, "unregistercallback ...");

Mtaskcallback = NULL;

}



@ Override

Public void registercallback (itaskcallback CB) throws RemoteException {

// Todo auto-generated method stub

Log. V (TAG, "registercallback ...");

Mtaskcallback = CB;

}



@ Override

Public void fuc01 () throws RemoteException {

// Todo auto-generated method stub

Log. V (TAG, "fuc01 ...");

}


@ Override

Public void fuc02 () throws RemoteException {

// Todo auto-generated method stub

Log. V (TAG, "fuc02 ...");

Mtaskcallback. onactionback ("Hello World ");

}


@ Override

Public String fuc03 (person) throws RemoteException {

// Todo auto-generated method stub

String name = person. getname ();

String descrip = person. getdescrip ();

Int sex = person. getsex ();

String ret = "";

If (sex = 0 ){

Ret = "hello" + name + ", you are handsome" + "(" + descrip + ")";

} Else {

Ret = "hello" + name + ", you are beautiful" + "(" + descrip + ")";

}

Return ret;

}

};


Callback interface implemented by the client

Private Final itaskcallback. Stub mcallback = new itaskcallback. Stub (){


@ Override

Public void onactionback (string Str) throws RemoteException {

// Todo auto-generated method stub

Log. V (TAG, "onactionback STR =" + Str );

}



};



How to bind a client to a server and establish a connection


Intent intent = new intent ("com. archermind. aidl. myservice ");

Bindservice (intent, mconnection, bind_auto_create );


Private serviceconnection mconnection = new serviceconnection (){


@ Override

Public void onserviceconnected (componentname name, ibinder Service ){

// Todo auto-generated method stub

Mservice = itaskbinder. stub. asinterface (service );

Try {

Mservice. registercallback (mcallback );

} Catch (RemoteException e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

}

Log. V (TAG, "onserviceconnected ");

}


For more information, see the source code project in the attachment.



Device-2012-09-29-153654.png (36.77
KB, downloads: 0)

Aidldemo.zip

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.