Aidl (2): transmitting complex objects

Source: Internet
Author: User
IPC transmits complex objects through aidl

1. Define data transmission objects


Person. aidl file:


In the person. Java file:

(1) Implement the parcelable Interface

(2) provide a static final attribute named creator

Package com. liujun. aidl;

Import Android. OS. parcel;

Import Android. OS. parcelable;

Public class person implements parcelable {

Private string name;

Private int sex;

Public Person (){

}

Public Person (parcel source ){

Readfromparcel (source );

}

// A static final attribute named creator must be provided. This attribute must implement the Android. OS. parcelable. creator <t> interface.

Public static final parcelable. creator <person> creator = new parcelable. creator <person> (){

@ Override

Public Person createfromparcel (parcel source ){

Return new person (source );

}

@ Override

Public person [] newarray (INT size ){

Return new person [size];

}

};

@ Override

Public int describecontents (){

Return 0;

}

// Note that the order of reading and writing variables should be the same, otherwise the correct result will not be obtained.

@ Override

Public void writetoparcel (parcel DEST, int flags ){

DeST. writestring (name );

DeST. writeint (sex );

}

// Note that the order of reading and writing variables should be the same, otherwise the correct result will not be obtained.

Public void readfromparcel (parcel source ){

Name = source. readstring ();

Sex = source. readint ();

}

//////////////////////////////////////// //////////////////////////

Public String getname (){

Return name;

}

Public void setname (string name ){

This. Name = Name;

}

Public int getsex (){

Return sex;

}

Public void setsex (INT sex ){

This. Sex = sex;

}

}

2. Define remote service interfaces and service components

Igreetservice. aidl file:

Package com. liujun. aidl;

Import com. liujun. aidl. person;

Interface igreetservice {

String greet (in person );

}

Aidlservice. Java file:

Package com. liujun. Service;

Import com. liujun. aidl. igreetservice;

Import com. liujun. aidl. person;

Import Android. App. Service;

Import Android. content. intent;

Import Android. OS. ibinder;

Import Android. OS. RemoteException;

Import Android. util. log;

Public class aidlservice extends Service {

Private Static final string tag = "liujun ";

 

@ Override

Public void oncreate (){

Log. I (TAG, "oncreate () called ");

Super. oncreate ();

}

 

@ Override

Public ibinder onbind (intent arg0 ){

Log. I (TAG, "onbind () called ");

Return stub;

}

// Service interface instance object

Igreetservice. Stub stub = new igreetservice. Stub (){

@ Override

Public String greet (person) throws RemoteException {

Log. I (TAG, "Greet (person) called ");

Return servicemethod (person );

}

};

@ Override

Public Boolean onunbind (intent ){

Log. I (TAG, "onunbind () called ");

Return true;

}

@ Override

Public void ondestroy (){

Super. ondestroy ();

Log. I (TAG, "ondestroy () called ");

}

/////////////////-----------------------------------

/**

* Service component method

* @ Param person

* @ Return

*/

Public String servicemethod (person ){

String greeting = "hello," + person. getname ();

 

Switch (person. getsex ()){

Case 0:

Greeting = greeting + ", you're handsome."; project code

Break;

Case 1:

Greeting = greeting + ", you're beautiful .";

Break;

}

Return greeting;

}

}

Registration Service:

<! -- Configure service components -->

<Service android: Name = "com. liujun. Service. aidlservice">

<Intent-filter>

<Action Android: Name = "android. Intent. Action. aidlservice"/>

<Category Android: Name = "android. Intent. Category. Default"/>

</Intent-filter>

</Service>

3. The client program copies the data transmission objects and interface files of the server program.


4. Bind Remote services, call remote service methods, and transmit complex objects

Package COM. liujun. parcelableclient; 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. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; import COM. example. parcelableclient. r; import COM. liujun. aidl. igreetservice; import COM. liujun. aidl. person; public class mainactivity extends activity {// control private button bindbtn; private button greetbtn; private button unbindbtn; private Boolean mbound = false; // whether to bind a remote service // The remote service interface object private igreetservice iservice; // obtain the remote service interface object private serviceconnection conn = new serviceconnection () {@ overridepublic void onserviceconnected (componentname name, ibinder Service) {iservice = igreetservice. stub. asinterface (service); mbound = true ;}@ overridepublic void evaluate (componentname name) {mbound = false; iservice = NULL ;}; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); bindbtn = (button) This. findviewbyid (R. id. bindbtn); greetbtn = (button) This. findviewbyid (R. id. greetbtn); unbindbtn = (button) This. findviewbyid (R. id. unbandbtn); // register and click the listener mylistener listener = new mylistener (); bindbtn. setonclicklistener (listener); greetbtn. setonclicklistener (listener); unbindbtn. setonclicklistener (listener);}/*** event processor * @ author Asus **/private class mylistener implements onclicklistener {@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. bindbtn: // bind the remote service intent = new intent ("android. intent. action. aidlservice "); bindservice (intent, Conn, context. bind_auto_create); // set the button status bindbtn. setenabled (false); greetbtn. setenabled (true); unbindbtn. setenabled (true); break; case R. id. greetbtn: Try {person = new person (); person. setname ("liujun"); person. setsex (0); string retval = iservice. greet (person); toast. maketext (mainactivity. this, retval, toast. length_short ). show ();} catch (RemoteException e) {toast. maketext (mainactivity. this, "error", toast. length_short ). show ();} break; case R. id. unbandbtn: unbindservice (conn); bindbtn. setenabled (true); greetbtn. setenabled (false); unbindbtn. setenabled (false); break ;}}@ overrideprotected void ondestroy () {super. ondestroy (); If (mbound) {unbindservice (conn); iservice = NULL ;}}}
Engineering Code http://download.csdn.net/detail/u010739551/7710977

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.