优点:Client端与Server端的DESCRIPTOR可以自定义,不受包名限制


实质中其实是使用底层Binder机制提供的Java层接口 Binder 、IInterface等去实现

客户端中使用transact发起进程间通信请求,服务端会回调onTransact来处理请求


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中片段:

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


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


更多相关文章

  1. okHttp3学习总结
  2. android简单的请求手机权限封装
  3. Android(安卓)2.2 API Demos -- 通过调用子Activity返回值
  4. Android(安卓)HttpClient用法
  5. Android(安卓)6.0以上系统使用RxPermissions申请动态权限
  6. android https 连接建立
  7. Android单元测试,模拟http的get和post请求,服务器脚本使用php
  8. 自带finish清除网络请求的Activity
  9. Android同步网络请求工具类HttpURLConnection

随机推荐

  1. Android Robotium的几个小tips
  2. ORB_SLAM2移植到Android,完整配置+填各种
  3. androidstudio使用kotlin开发APP
  4. Android开发常见“疑难杂症”解决方案汇
  5. android 文件的读取 附源码
  6. 你真的理解android事件分发机制了吗
  7. android 杂记-- adb 使用笔记
  8. Android(安卓)build文件的删除的方法
  9. Design Support Library
  10. Linux常用基本命令&Android系统编译命令