Remote invocation of service not implemented in Android using Aidl

Source: Internet
Author: User

Advantage : Client side and server side descriptor can be customized, not limited by package name


In essence, the Java Layer Interface Binder, IInterface, etc. are provided using the underlying binder mechanism to implement

The client uses Transact to initiate interprocess communication requests, and the server will callback ontransact to process the request


Common Interface:



Server:

Public abstract class Timecountstub extends Android.os.Binder Implementsiinterface, Itimecountservice {private static Final java.lang.String descriptor = "Com.example.servicedemo.nonaidl.ITimeCountService";/** Construct the stub at Attach it to the interface. */public timecountstub () {this.attachinterface (this, descriptor);} @Overridepublic Android.os.IBinder Asbinder () {return this;} @Overridepublic boolean ontransact (int code, android.os.Parcel data,android.os.parcel reply, int flags) throws android.os.RemoteException {switch (code) {case Interface_transaction: {reply.writestring (descriptor); return true;} Case Transaction_getcount: {data.enforceinterface (descriptor); int _result = This.getcount (); Reply.writenoexception ( ); Reply.writeint (_result); return true;}} Return Super.ontransact (Code, data, reply, flags);} static final int transaction_getcount = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 0);}


Client:

public class Timecountproxy implements/*iinterface,*/Itimecountservice {private static final java.lang.String Descriptor = "Com.example.servicedemo.nonaidl.ITimeCountService";p rivate android.os.IBinder mremote; Timecountproxy (Android.os.IBinder remote) {mremote = remote;} @Override//public Android.os.IBinder Asbinder () {//return mremote;//}public java.lang.String Getinterfacedescriptor () {return descriptor;} /** * Cast an IBinder object to an * Com.example.servicedemo.nonaidl.ITimeCountService interface, generating a * proxy I F needed. */public static Itimecountservice asinterface (Android.os.IBinder obj) {if ((obj = = null)) {return null;} Android.os.IInterface iin = obj.querylocalinterface (descriptor); if ((iin! = null) && (iin instanceof Itimecountservice)) {return ((Itimecountservice) iin);} return new Timecountproxy (obj); @Overridepublic int GetCount () throws android.os.RemoteException {Android.os.Parcel _data = Android.os.Parcel.obtain () ; Android.os.Parcel _reply = Android. os. Parcel.obtain (); int _result;try {_data.writeinterfacetoken (descriptor); Mremote.transact ( Timecountstub.transaction_getcount, _data, _reply,0); _reply.readexception (); _result = _reply.readint ();} finally {_reply.recycle (); _data.recycle ();} return _result;}}


Segments in service:

public class Timecountservice extends Service {... Timecountstub stub = new Timecountstub () {@Overridepublic int getcount () throws RemoteException {return count;}}; @Overridepublic ibinder onbind (Intent Intent) {log.i (TAG, "Onbind"); return stub;} ...}


Fragment in Client:

public class Mainactivity extends Activity {... private itimecountservice timecountservice;private serviceconnection conn = new Serviceconnection () {@Overridepublic void onserviceconnected (componentname name, IBinder service) {log.i (TAG, "onserviceconnected"); timecountservice = timecountproxy.asinterface (service); LOG.I (TAG, "Timecountservice:" + timecountservice); cantimeupdatetaskrunning = true; Timeupatetask t = new Timeupatetask (); T.execute (new Object ()); LOG.I (TAG, "task status:" + T.getstatus ());} @Overridepublic void onservicedisconnected (componentname name) {log.i (TAG, "onservicedisconnected");}}; ...}


Remote invocation of service not implemented in Android using Aidl

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.