Android Service的思考(4)
在android平台中,一个进程通常不能访问其他进程中的内存区域的。但是,我们可以使用IDL语言来把对象伪装成操作系统能理解的简单形式,以便伪装成对象跨越边界访问。 如果想在应用程序中调用其他进程中的Service,则需要用到AIDL,AIDL(android接口描述语言)是一种IDL语言,它可以生成一段代码,可以使在一个android设备上运行的两个进程使用内部通信进程进行交互。如果你需要在一个进程中(例如:在一个Activity中)访问另一个进程中(例如:一个Service)某个对象的方法,你就可以使用AIDL来生成这样的代码来伪装传递各种参数。 使用AIDL的方法如下: 1.首先生成一个IMusicService.aidl的服务接口,Android会自动生成一个 Stub类,这个类继承了BInder类,同时继承了IMusicService这个接口,还可以看到其中包含了一个Proxy代理类,以实现远程代理。(aidl和Stub类如下所示) ?[Copy to clipboard] Downloadzuiniuwang.java /** *IMusicService.aidl *com.androidtest.service.mediaplayer * *Function:TODO * *verdateauthor *────────────────────────────────── *2011-5-19Leon * *Copyright(c)2011,TNTAllRightsReserved. */ packagecom.androidtest.service.mediaplayer; /** *ClassName:IMusicService *Function:TODOADDFUNCTION *Reason:TODOADDREASON * *@authorLeon *@version *@sinceVer1.1 *@Date2011-5-19 */ interfaceIMusicService{ voidplay(); voidpause(); voidstop(); } /* *Thisfileisauto-generated.DONOTMODIFY. *Originalfile:D:\\Backup\\�ҵ��ĵ�\\Dropbox\\investment\\A8\\workspace\\androidtest\\src\\com\\androidtest\\service\\mediaplayer\\IMusicService.aidl */ packagecom.androidtest.service.mediaplayer; /** *ClassName:IMusicServiceFunction:TODOADDFUNCTIONReason:TODOADDREASON * *@authorLeon *@version *@sinceVer1.1 *@Date2011-5-19 */ publicinterfaceIMusicServiceextendsandroid.os.IInterface{ /**Local-sideIPCimplementationstubclass.*/ publicstaticabstractclassStubextendsandroid.os.Binderimplements com.androidtest.service.mediaplayer.IMusicService{ privatestaticfinaljava.lang.StringDESCRIPTOR="com.androidtest.service.mediaplayer.IMusicService"; /**Constructthestubatattachittotheinterface.*/ publicStub(){ this.attachInterface(this,DESCRIPTOR); } /** *CastanIBinderobjectintoan *com.androidtest.service.mediaplayer.IMusicServiceinterface, *generatingaproxyifneeded. */ publicstaticcom.androidtest.service.mediaplayer.IMusicServiceasInterface( android.os.IBinderobj){ if((obj==null)){ returnnull; } android.os.IInterfaceiin=(android.os.IInterface)obj .queryLocalInterface(DESCRIPTOR); if(((iin!=null)&&(iininstanceofcom.androidtest.service.mediaplayer.IMusicService))){ return((com.androidtest.service.mediaplayer.IMusicService)iin); } returnnewcom.androidtest.service.mediaplayer.IMusicService.Stub.Proxy( obj); } publicandroid.os.IBinderasBinder(){ returnthis; } @Override publicbooleanonTransact(intcode,android.os.Parceldata, android.os.Parcelreply,intflags) throwsandroid.os.RemoteException{ switch(code){ caseINTERFACE_TRANSACTION:{ reply.writeString(DESCRIPTOR); returntrue; } caseTRANSACTION_play:{ data.enforceInterface(DESCRIPTOR); this.play(); reply.writeNoException(); returntrue; } caseTRANSACTION_pause:{ data.enforceInterface(DESCRIPTOR); this.pause(); reply.writeNoException(); returntrue; } caseTRANSACTION_stop:{ data.enforceInterface(DESCRIPTOR); this.stop(); reply.writeNoException(); returntrue; } } returnsuper.onTransact(code,data,reply,flags); } privatestaticclassProxyimplements com.androidtest.service.mediaplayer.IMusicService{ privateandroid.os.IBindermRemote; Proxy(android.os.IBinderremote){ mRemote=remote; } publicandroid.os.IBinderasBinder(){ returnmRemote; } publicjava.lang.StringgetInterfaceDescriptor(){ returnDESCRIPTOR; } publicvoidplay()throwsandroid.os.RemoteException{ android.os.Parcel_data=android.os.Parcel.obtain(); android.os.Parcel_reply=android.os.Parcel.obtain(); try{ _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_play,_data,_reply,0); _reply.readException(); }finally{ _reply.recycle(); _data.recycle(); } } publicvoidpause()throwsandroid.os.RemoteException{ android.os.Parcel_data=android.os.Parcel.obtain(); android.os.Parcel_reply=android.os.Parcel.obtain(); try{ _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_pause,_data,_reply,0); _reply.readException(); }finally{ _reply.recycle(); _data.recycle(); } } publicvoidstop()throwsandroid.os.RemoteException{ android.os.Parcel_data=android.os.Parcel.obtain(); android.os.Parcel_reply=android.os.Parcel.obtain(); try{ _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_stop,_data,_reply,0); _reply.readException(); }finally{ _reply.recycle(); _data.recycle(); } } } staticfinalintTRANSACTION_play=(android.os.IBinder.FIRST_CALL_TRANSACTION+0); staticfinalintTRANSACTION_pause=(android.os.IBinder.FIRST_CALL_TRANSACTION+1); staticfinalintTRANSACTION_stop=(android.os.IBinder.FIRST_CALL_TRANSACTION+2); } publicvoidplay()throwsandroid.os.RemoteException; publicvoidpause()throwsandroid.os.RemoteException; publicvoidstop()throwsandroid.os.RemoteException; } 2. 在MyRemoteBinder需要继承这个Stub类,对播放器的控制写在这个Binder类中 ?[Copy to clipboard] Downloadzuiniuwang.java /** *MyRemoteBinder.java *com.androidtest.service.mediaplayer * *Function:TODO * *verdateauthor *────────────────────────────────── *2011-5-19Leon * *Copyright(c)2011,TNTAllRightsReserved. */ packagecom.androidtest.service.mediaplayer; importandroid.media.MediaPlayer; importandroid.os.Binder; importandroid.os.RemoteException; /** *ClassName:MyRemoteBinder *Function:TODOADDFUNCTION *Reason:TODOADDREASON * *@authorLeon *@version *@sinceVer1.1 *@Date2011-5-19 */ publicclassMyRemoteBinderextendsIMusicService.Stub{ publicMyRemoteBinder(MediaPlayermediaPlayer){ MyMediaController.mediaPlayer=mediaPlayer; }; @Override publicvoidplay()throwsRemoteException{ //TODOAuto-generatedmethodstub MyMediaController.play.execute(); } @Override publicvoidpause()throwsRemoteException{ //TODOAuto-generatedmethodstub MyMediaController.pause.execute(); } @Override publicvoidstop()throwsRemoteException{ //TODOAuto-generatedmethodstub MyMediaController.stop.execute(); }; } 3. 在Activity中得到Binder的方式,是通过Stub类的IMusicService.Stub.asInterface(binder)方法 ?[Copy to clipboard] Downloadzuiniuwang.java privateServiceConnectionmyServiceConnection=newServiceConnection(){ @Override publicvoidonServiceConnected(ComponentNamename,IBinderbinder){ musicServiceInterface=IMusicService.Stub.asInterface(binder); Log.d(TAG,"onServiceConnected"); } @Override publicvoidonServiceDisconnected(ComponentNamename){ musicServiceInterface=null; Log.d(TAG,"onServiceDisconnected"); } }; 本文转自 最牛傻蛋 51CTO博客,原文链接:http://blog.51cto.com/zuiniuwang/718309,如需转载请自行联系原作者