Remote Service calling is not implemented using AIDL in Android.

Source: Internet
Author: User
Tags mremote

Remote Service calling is not implemented using AIDL in Android.

Advantages: The DESCRIPTOR on the Client and Server can be customized without the package name restriction.


In essence, it is implemented by using Java layer interfaces such as Binder and IInterface provided by the underlying Binder Mechanism.

When the client initiates an inter-process communication request using transact, the server calls back onTransact to process the request.


Common Interface:

public interface ITimeCountService {int getCount() throws RemoteException;} 


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";private 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 into an * com.example.servicedemo.nonaidl.ITimeCountService interface, generating a * proxy if 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;}}


Service segment:

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;}...}


Segment 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");}};...}


Related Article

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.