首页 > 代码库 > Android中不使用AIDL实现Service的远程调用
Android中不使用AIDL实现Service的远程调用
优点: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 implements IInterface, 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); } @Override public android.os.IBinder asBinder() { return this; } @Override public 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); } @Override public int getCount() throws android.os.RemoteException { android.os.Parcel _data = http://www.mamicode.com/android.os.Parcel.obtain();>Service中片段:
public class TimeCountService extends Service { ... TimeCountStub stub = new TimeCountStub() { @Override public int getCount() throws RemoteException { return count; } }; @Override public IBinder onBind(Intent intent) { Log.i(TAG, "onBind"); return stub; } ... }Client中片段:
public class MainActivity extends Activity { ... private ITimeCountService timeCountService; private ServiceConnection conn = new ServiceConnection() { @Override public 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()); } @Override public void onServiceDisconnected(ComponentName name) { Log.i(TAG, "onServiceDisconnected"); } }; ... }Android中不使用AIDL实现Service的远程调用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。