In addition to providing similar services such as music playing through the service we mentioned in the previous article, we can also pass the object back through the service. Do you know how to use it? Let's look at the example first:
When we click "get", an object will be retrieved from the service of another thread and the content will be read out.
Bytes --------------------------------------------------------------------------------------------
Aidl implements object-based interaction. The main steps are as follows:
Server:
First, define an object class. Here is beauty, which defines a service interface aidl file remotebeauty. aidl, here we need to note that we need to import the package through import When referencing the custom object class to aidl, but you will find that even if you import the package, you will still be prompted not to find it, at this time, you need to create an aidl file named after the object class name, such as beauty. aidl: Add pracelable beauty to it.
Second: start to write beauty. Note that it must implement the pracelable interface, which is a serialized interface with similar functions as serializable, but with more rapid functions. In addition, you must declare a public static final pracelable. creator <t> creator object in this beauty !! Except that the T in it represents the entity class, nothing else can be changed.
Third: register the service in androidmanifest. xml. Define the action string to access the service.
Client:
The client side is much simpler, but note that you need to copy all the object classes and aidl files, and ensure that the paths are completely consistent !!
-------------------------------------------------------------
Sample Code:
Server:
Beauty. Java
Package cn.com. chenzheng_java.service; </P> <p> Import android. OS. parcel; <br/> Import android. OS. parcelable; <br/>/** <br/> * @ author chenzheng_java <br/> * @ description parcelable is a serial number interface provided by Android, which is more efficient than serializable. <br/> * parcelable must be inherited here, how can I pass a serial number without a serial number ...... Right ?! <Br/> * in the object class, we need to do two important tasks: <br/> * 1: Implementing the parcelable interface <br/> * 2: defining a parcelable. creator object of the Creator type <br/> * Third: provide a beauty. in the aidl file, the content is parcelable beauty. After definition, if beauty is referenced in other aidl files, no error is prompted. <Br/> * @ since 2011/03/18 <br/> */<br/> public class beauty implements parcelable {</P> <p> string name; <br/> int age; <br/> string sex; </P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public int getage () {<br/> return age; <br/>}</P> <p> Public void setage (INT age) {<br/> This. age = age; <br/>}</P> <p> publi C string getsex () {<br/> return sex; <br/>}</P> <p> Public void setsex (string sex) {<br/> This. sex = sex; <br/>}</P> <p> @ override <br/> Public int describecontents () {<br/> return 0; <br/>}</P> <p>/** <br/> * set the object serial number <br/> * DEST is the target object to be written to. <br/> * flags: identifier of the object serial number <br/> * Note that, the write order must be exactly the same as that read in the createfromparcel method. For example, name is first written here, <br/> * In createfromparcel, you must first read the name <br/> */<br/> @ override <br/> Public void writetoparcel (parcel DEST, int flags) {</P> <p> DeST. writestring (name); <br/> DeST. writeint (AGE); <br/> DeST. writestring (sex); <br/>}< br/>/** <br/> * this constant must be declared inside the entity class for serial number transfer. The constant name can only be a creator, and the type must also be <br/> * parcelable. creator <t> <br/> */<br/> Public static final parcelable. creator <beauty> creator = new creator <beauty> () {</P> <p>/** <br/> * Create an array of entity classes to be serial numbers, all files stored in the array are set to null <br/> */<br/> @ override <br/> Public beauty [] newarray (INT size) {<br/> return new beauty [size]; <br/>}</P> <p>/*** <br/> * indicates the parcel object based on the serial number, the reverse serial number is the original object. <br/> * The read sequence must be the same as that of writetoparcel. <br/> */<br/> @ override <br/> Public beauty createfromparcel (parcel source) {<br/> string name = source. readstring (); <br/> int age = source. readint (); <br/> string sex = source. readstring (); <br/> Beauty = new beauty (); <br/> beauty. setname (name); <br/> beauty. setage (AGE); <br/> beauty. setsex (sex); </P> <p> return beauty; <br/>}< br/>}; </P> <p >}< br/>
Remoteservice. Java
Package cn.com. chenzheng_java.service; </P> <p> Import android. app. service; <br/> Import android. content. intent; <br/> Import android. OS. ibinder; <br/> Import android. OS. remoteException; <br/> Import android. util. log; <br/>/** <br/> * @ author chenzheng_java <br/> * @ description service <br/> * <br/> */<br/> public class remoteservice extends Service {</P> <p> @ override <br/> Public ibinder onbind (intent) {<br/> log. I ("notification", "onbind executed"); <br/> return New mybinder (); <br/>}</P> <p> private class mybinder extends remotebeauty. stub {</P> <p> @ override <br/> Public beauty getbeauty () throws RemoteException {</P> <p> Beauty = new beauty (); <br/> beauty. setname ("Feifei"); <br/> beauty. setage (21); <br/> beauty. setsex ("female"); </P> <p> return beauty; <br/>}</P> <p >}< br/>
Serviceactivity. Java
Package cn.com. chenzheng_java.service; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/>/** <br/> * @ description transmission of object data between processes <br/> * @ author chenzheng_java <br/> */< br/> public class serviceactivity extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/>}< br/>}
Beauty. aidl
Parcelable beauty;
Remotebeauty. aidl
Package cn.com. chenzheng_java.service; <br/> Import cn.com. chenzheng_java.service.beauty; <br/> interface remotebeauty {</P> <p> beauty getbeauty (); </P> <p >}< br/>
Manifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "cn.com. chenzheng_java.service "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> </P> <p> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". serviceactivity "<br/> Android: Label =" @ string/app_name "> <Br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> </P> <p> <! -- Service start --> <br/> <service android: Name = "remoteservice"> <br/> <intent-filter> <br/> <action Android: name = "cn.com. chenzheng_java.remote2 "/> <br/> </intent-filter> <br/> </service> <br/> <! -- Service ends --> <br/> </Application> <br/> </manifest>
-------------------------------------
Client:
Clientactivity. Java
Package cn.com. chenzheng_java.client; </P> <p> Import android. app. activity; <br/> Import android. content. componentname; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. serviceconnection; <br/> Import android. OS. bundle; <br/> Import android. OS. ibinder; <br/> Import android. OS. remoteException; <br/> Import android. util. log; <br/> Import android. view. view; <B R/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. textview; <br/> Import cn.com. chenzheng_java.service.beauty; <br/> Import cn.com. chenzheng_java.service.remotebeauty; </P> <p> public class clientactivity extends activity implements onclicklistener {<br/> textview; <br/> button; <br/> string actionname = "cn.com. chenzheng_java.re Mote2 "; <br/> remotebeauty; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> textview = (textview) findviewbyid (R. id. textview1); <br/> button = (button) findviewbyid (R. id. button1); </P> <p> button. setonclicklistener (this); <br/>}</P> <p> private class myserviceconnection implement S serviceconnection {</P> <p> @ override <br/> Public void onserviceconnected (componentname, ibinder Service) {<br/> log. I ("notification", "link successful! "); <Br/> remotebeauty = remotebeauty. stub. asinterface (service); <br/> try {<br/> Beauty = remotebeauty. getbeauty (); <br/> textview. settext ("Beauty name:" + beauty. getname () + "Age:" + beauty. getage () + "Gender:" + beauty. getsex (); </P> <p >}catch (RemoteException e) {<br/> E. printstacktrace (); <br/>}</P> <p> @ override <br/> Public void onservicedisconnected (componentname name) {</P> <p >}< br/> myserviceconnection connection = new myserviceconnection (); <br/> @ override <br/> Public void onclick (view v) {<br/> intent = new intent (actionname); <br/> bindservice (intent, connection, context. bind_auto_create); <br/>}< br/>}
In addition, Beauty. Java and remotebeauty. aidl are all copied from the server system.
If you want your service to start automatically when the system starts up. You can add this configuration in service androidmanifest. xml.
<Cycler Android: Name = ". startbroadcastreceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
</Cycler>