/** * Example of defining an interface for calling on to a remote service * (running in another process). */interfaceIRemoteService{/** * Often you want to allow a service to call back to its clients. * This shows how to do so, by registering a callback interface with * the service. */voidregisterCallback(IRemoteServiceCallbackcb);/** * Remove a previously registered callback interface. */voidunregisterCallback(IRemoteServiceCallbackcb);}
publicinterfaceIRemoteServiceextendsandroid.os.IInterface{/** Default implementation for IRemoteService. */publicstaticclassDefaultimplementscn.malinkang.servicesamples.IRemoteService{/** * Often you want to allow a service to call back to its clients. * This shows how to do so, by registering a callback interface with * the service. */@OverridepublicvoidregisterCallback(cn.malinkang.servicesamples.IRemoteServiceCallbackcb)throwsandroid.os.RemoteException{}/** * Remove a previously registered callback interface. */@OverridepublicvoidunregisterCallback(cn.malinkang.servicesamples.IRemoteServiceCallbackcb)throwsandroid.os.RemoteException{}@Overridepublicandroid.os.IBinderasBinder(){returnnull;}}/** Local-side IPC implementation stub class. */publicstaticabstractclassStubextendsandroid.os.Binderimplementscn.malinkang.servicesamples.IRemoteService{privatestaticfinaljava.lang.StringDESCRIPTOR="cn.malinkang.servicesamples.IRemoteService";/** Construct the stub at attach it to the interface. */publicStub(){this.attachInterface(this,DESCRIPTOR);}/** * Cast an IBinder object into an cn.malinkang.servicesamples.IRemoteService interface, * generating a proxy if needed. *///onServiceConnected调用 传入onServiceConnected返回的Binderpublicstaticcn.malinkang.servicesamples.IRemoteServiceasInterface(android.os.IBinderobj){if((obj==null)){returnnull;}//查询android.os.IInterfaceiin=obj.queryLocalInterface(DESCRIPTOR);//同一个进程if(((iin!=null)&&(iininstanceofcn.malinkang.servicesamples.IRemoteService))){return((cn.malinkang.servicesamples.IRemoteService)iin);}//不同进程returnnewcn.malinkang.servicesamples.IRemoteService.Stub.Proxy(obj);}//返回当前对象@Overridepublicandroid.os.IBinderasBinder(){returnthis;}@OverridepublicbooleanonTransact(intcode,android.os.Parceldata,android.os.Parcelreply,intflags)throwsandroid.os.RemoteException{java.lang.Stringdescriptor=DESCRIPTOR;switch(code){caseINTERFACE_TRANSACTION:{reply.writeString(descriptor);returntrue;}caseTRANSACTION_registerCallback:{data.enforceInterface(descriptor);cn.malinkang.servicesamples.IRemoteServiceCallback_arg0;_arg0=cn.malinkang.servicesamples.IRemoteServiceCallback.Stub.asInterface(data.readStrongBinder());this.registerCallback(_arg0);reply.writeNoException();returntrue;}caseTRANSACTION_unregisterCallback:{data.enforceInterface(descriptor);cn.malinkang.servicesamples.IRemoteServiceCallback_arg0;_arg0=cn.malinkang.servicesamples.IRemoteServiceCallback.Stub.asInterface(data.readStrongBinder());this.unregisterCallback(_arg0);reply.writeNoException();returntrue;}default:{returnsuper.onTransact(code,data,reply,flags);}}}privatestaticclassProxyimplementscn.malinkang.servicesamples.IRemoteService{privateandroid.os.IBindermRemote;Proxy(android.os.IBinderremote){mRemote=remote;}@Overridepublicandroid.os.IBinderasBinder(){returnmRemote;}publicjava.lang.StringgetInterfaceDescriptor(){returnDESCRIPTOR;}/** * Often you want to allow a service to call back to its clients. * This shows how to do so, by registering a callback interface with * the service. */@OverridepublicvoidregisterCallback(cn.malinkang.servicesamples.IRemoteServiceCallbackcb)throwsandroid.os.RemoteException{android.os.Parcel_data=android.os.Parcel.obtain();android.os.Parcel_reply=android.os.Parcel.obtain();try{_data.writeInterfaceToken(DESCRIPTOR);_data.writeStrongBinder((((cb!=null))?(cb.asBinder()):(null)));boolean_status=mRemote.transact(Stub.TRANSACTION_registerCallback,_data,_reply,0);if(!_status&&getDefaultImpl()!=null){getDefaultImpl().registerCallback(cb);return;}_reply.readException();}finally{_reply.recycle();_data.recycle();}}/** * Remove a previously registered callback interface. */@OverridepublicvoidunregisterCallback(cn.malinkang.servicesamples.IRemoteServiceCallbackcb)throwsandroid.os.RemoteException{android.os.Parcel_data=android.os.Parcel.obtain();android.os.Parcel_reply=android.os.Parcel.obtain();try{_data.writeInterfaceToken(DESCRIPTOR);_data.writeStrongBinder((((cb!=null))?(cb.asBinder()):(null)));boolean_status=mRemote.transact(Stub.TRANSACTION_unregisterCallback,_data,_reply,0);if(!_status&&getDefaultImpl()!=null){getDefaultImpl().unregisterCallback(cb);return;}_reply.readException();}finally{_reply.recycle();_data.recycle();}}publicstaticcn.malinkang.servicesamples.IRemoteServicesDefaultImpl;}staticfinalintTRANSACTION_registerCallback=(android.os.IBinder.FIRST_CALL_TRANSACTION+0);staticfinalintTRANSACTION_unregisterCallback=(android.os.IBinder.FIRST_CALL_TRANSACTION+1);publicstaticbooleansetDefaultImpl(cn.malinkang.servicesamples.IRemoteServiceimpl){// Only one user of this interface can use this function// at a time. This is a heuristic to detect if two different// users in the same process use this function.if(Stub.Proxy.sDefaultImpl!=null){thrownewIllegalStateException("setDefaultImpl() called twice");}if(impl!=null){Stub.Proxy.sDefaultImpl=impl;returntrue;}returnfalse;}publicstaticcn.malinkang.servicesamples.IRemoteServicegetDefaultImpl(){returnStub.Proxy.sDefaultImpl;}}/** * Often you want to allow a service to call back to its clients. * This shows how to do so, by registering a callback interface with * the service. */publicvoidregisterCallback(cn.malinkang.servicesamples.IRemoteServiceCallbackcb)throwsandroid.os.RemoteException;/** * Remove a previously registered callback interface. */publicvoidunregisterCallback(cn.malinkang.servicesamples.IRemoteServiceCallbackcb)throwsandroid.os.RemoteException;}
/** * The IRemoteInterface is defined through IDL */privatefinalIRemoteService.StubmBinder=newIRemoteService.Stub(){publicvoidregisterCallback(IRemoteServiceCallbackcb){if(cb!=null)mCallbacks.register(cb);}publicvoidunregisterCallback(IRemoteServiceCallbackcb){if(cb!=null)mCallbacks.unregister(cb);}};
publicclassRemoteServiceextendsService{@OverridepublicvoidonCreate(){super.onCreate();}@OverridepublicIBinderonBind(Intentintent){// Return the interfacereturnbinder;}privatefinalIRemoteService.Stubbinder=newIRemoteService.Stub(){publicintgetPid(){returnProcess.myPid();}publicvoidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,doubleaDouble,StringaString){// Does nothing}};}
IRemoteServiceiRemoteService;privateServiceConnectionmConnection=newServiceConnection(){// Called when the connection with the service is establishedpublicvoidonServiceConnected(ComponentNameclassName,IBinderservice){// Following the example above for an AIDL interface,// this gets an instance of the IRemoteInterface, which we can use to call on the serviceiRemoteService=IRemoteService.Stub.asInterface(service);}// Called when the connection with the service disconnects unexpectedlypublicvoidonServiceDisconnected(ComponentNameclassName){Log.e(TAG,"Service has unexpectedly disconnected");iRemoteService=null;}};
// IRectInsideBundle.aidlpackagecom.example.android;/** Example service interface */interfaceIRectInsideBundle{/** Rect parcelable is stored in the bundle with key "rect" */voidsaveRect(inBundlebundle);}
privatefinalIRectInsideBundle.Stubbinder=newIRectInsideBundle.Stub(){publicvoidsaveRect(Bundlebundle){bundle.setClassLoader(getClass().getClassLoader());Rectrect=bundle.getParcelable("rect");process(rect);// Do more with the parcelable.}};