生活随笔
收集整理的這篇文章主要介紹了
Android Service(7)--完结篇
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傻蛋在Android Service(4) 中講述了使用AIDL語言,來讓ADT幫助我們自動生成一個Stub類(Binder的子類),來實現不同進程中Service的調用。通過研究ADT自動生成的代碼可以發現,這段代碼其實是幫我們實現了一個代理類,這個代理能夠通過進行不同進程間的通信。我們Activity調用這個代理類,而這個調用遠程Service中的方法。從而實現了遠程通信。
今天傻蛋自己寫了一個底層遠程通信的代理類(具體通信方式傻蛋在第6篇中已經講了),同樣實現了遠程調用Service的效果。基本結構和ADT自動生成的代碼相似,但是清晰了很多,使用方法和第四篇中的使用一某一樣,十分通用。
?
?[Copy to clipboard]Download IMyMusicService.java /** ??*?IMusicService.java ??*?com.androidtest.service.mediaplayer ??* ??*?Function:?TODO ??* ??*???ver?????date???????????author ??*?────────────────────────────────── ??*???????????2011-6-22??????Leon ??* ??*?Copyright?(c)?2011,?TNT?All?Rights?Reserved. ??*/ ?? ?package?com.androidtest.service.remoteplayer; ?? ?import?android.os.Binder; ?import?android.os.IBinder; ?import?android.os.Parcel; ?import?android.os.RemoteException; ?import?android.util.Log; ?? ?/** ??*?ClassName:IMusicService?Function:?TODO?ADD?FUNCTION?Reason:?TODO?ADD?REASON ??* ??*?@author?Leon ??*?@version ??*?@since?Ver?1.1 ??*?@Date?2011-6-22 ??*/ ?public?interface?IMyMusicService?{ ?? ?????static?final?String?TAG?=?IMyMusicService.class.getSimpleName(); ?????static?final?int?TRANSACTION_PLAY?=?android.os.IBinder.FIRST_CALL_TRANSACTION?+?0; ?????static?final?int?TRANSACTION_PAUSE?=?android.os.IBinder.FIRST_CALL_TRANSACTION?+?1; ?????static?final?int?TRANSACTION_STOP?=?android.os.IBinder.FIRST_CALL_TRANSACTION?+?2; ?? ?????public?void?play()?throws?RemoteException?; ?? ?????public?void?pause()throws?RemoteException?; ?? ?????public?void?stop()?throws?RemoteException?; ?? ?????public?static?abstract?class?Stub?extends?Binder?implements?IMyMusicService?{ ?????????//用于Service的接收 ?????????@Override ?????????protected?boolean?onTransact(int?code,?Parcel?data,?Parcel?reply, ?????????????????int?flags)?throws?RemoteException?{ ?? ?????????????//?TODO?Auto-generated?method?stub ?????????????switch?(code)?{ ?????????????case?TRANSACTION_PLAY: ?????????????????this.play(); ?????????????????/** ??????????????????*?注意?readString?是這樣解釋 ??????????????????*?Read?a?string?value?from?the?parcel?at?the?current?dataPosition(). ??????????????????*?第二次讀的話data?position就會前移,此處就為null了 ??????????????????*/ ?????????????????String?temp?=?data.readString(); ?????????????????Log.v(TAG,"The?Message?From??Clinet?:?"+temp); ?????????????????reply.writeString(temp); ?????????????????return?true; ?????????????case?TRANSACTION_PAUSE: ?????????????????this.pause(); ?????????????????Log.v(TAG,"The?Message?From??Clinet?:?"+data.readString()); ?????????????????reply.writeString(data.readString()); ?????????????????return?true; ?????????????case?TRANSACTION_STOP: ?????????????????this.stop(); ?????????????????Log.v(TAG,"The?Message?From??Clinet?:"+data.readString()); ?????????????????reply.writeString(data.readString()); ?????????????????return?true; ?????????????} ?????????????return?super.onTransact(code,?data,?reply,?flags); ?? ?????????} ?????????//模板模式 ?????????public?abstract?void?play()?throws?RemoteException?; ?? ?????????public?abstract?void?pause()?throws?RemoteException?; ?? ?????????public?abstract?void?stop()?throws?RemoteException?; ?? ?????????//其實就是獲取代理類,供Activity來調用 ?????????public?static?IMyMusicService?asInterface(IBinder?iBinder){ ?????????????return?new?Proxy(iBinder); ?????????} ?? ?????????//Activity其實就是通過此代理和遠程的Stub進行通信 ?????????private?static?class?Proxy?implements?IMyMusicService{ ?????????????private?IBinder?iBinder; ?????????????public??Proxy(IBinder?iBinder)?{ ?????????????????this.iBinder?=iBinder; ?????????????} ?? ?????????????@Override ?????????????public?void?pause()?{ ?????????????????Parcel?sendData?=?Parcel.obtain(); ?????????????????Parcel?replyData?=Parcel.obtain(); ?????????????????sendData.writeString("pause"); ?????????????????try?{ ?????????????????????iBinder.transact(TRANSACTION_PAUSE,?sendData,?replyData,?0); ?????????????????????Log.v(TAG,?"The?Message?From?Service?"?+?replyData.readString()?); ?????????????????}?catch?(RemoteException?e)?{ ?????????????????????//?TODO?Auto-generated?catch?block ?????????????????????e.printStackTrace(); ?????????????????} ?????????????????//?TODO?Auto-generated?method?stub ?????????????} ?? ?????????????@Override ?????????????public?void?play()?{ ?? ?????????????????//?TODO?Auto-generated?method?stub ?????????????????Parcel?sendData?=?Parcel.obtain(); ?????????????????Parcel?replyData?=Parcel.obtain(); ?????????????????sendData.writeString("play"); ?????????????????try?{ ?????????????????????iBinder.transact(TRANSACTION_PLAY,?sendData,?replyData,?0); ?????????????????????Log.v(TAG,?"The?Message?From?Service?"?+?replyData.readString()); ?????????????????}?catch?(RemoteException?e)?{ ?????????????????????//?TODO?Auto-generated?catch?block ?????????????????????e.printStackTrace(); ?????????????????} ?? ?????????????} ?? ?????????????@Override ?????????????public?void?stop()?{ ?? ?????????????????//?TODO?Auto-generated?method?stub ?????????????????Parcel?sendData?=?Parcel.obtain(); ?????????????????Parcel?replyData?=Parcel.obtain(); ?????????????????sendData.writeString("stop"); ?????????????????try?{ ?????????????????????iBinder.transact(TRANSACTION_STOP,?sendData,?replyData,?0); ?????????????????????Log.v(TAG,?"The?Message?From?Service?"?+?replyData.readString()?); ?????????????????}?catch?(RemoteException?e)?{ ?????????????????????//?TODO?Auto-generated?catch?block ?????????????????????e.printStackTrace(); ?????????????????} ?? ?????????????} ?? ?????????} ?? ?????} ?}? ?[Copy to clipboard]Download MyCustomRemoteBinder.java /** ??*?MyRemoteBinder.java ??*?com.androidtest.service.remoteplayer ??* ??*?Function:?TODO ??* ??*???ver?????date???????????author ??*?────────────────────────────────── ??*???????????2011-6-22??????Leon ??* ??*?Copyright?(c)?2011,?TNT?All?Rights?Reserved. ?*/ ?? ?package?com.androidtest.service.remoteplayer; ?? ?import?android.media.MediaPlayer; ?import?android.os.RemoteException; ?? ?import?com.androidtest.service.mediaplayer.MyMediaController; ?? ?/** ??*?ClassName:MyRemoteBinder ??*?Function:?TODO?ADD?FUNCTION ??*?Reason:???TODO?ADD?REASON ??* ??*?@author???Leon ??*?@version ??*?@since????Ver?1.1 ??*?@Date?????2011-6-22 ??*/ ?public?class?MyCustomRemoteBinder?extends?IMyMusicService.Stub{ ?? ?????public??MyCustomRemoteBinder(MediaPlayer?mediaPlayer){ ?????????MyMediaController.mediaPlayer=mediaPlayer?; ?????}; ?????@Override ?????public?void?play()?throws?RemoteException?{ ?? ?????????//?TODO?Auto-generated?method?stub ?????????MyMediaController.play.execute(); ?? ?????} ?? ?????@Override ?????public?void?pause()?throws?RemoteException?{ ?? ?????????//?TODO?Auto-generated?method?stub ?????????MyMediaController.pause.execute(); ?? ?????} ?? ?????@Override ?????public?void?stop()?throws?RemoteException?{ ?? ?????????//?TODO?Auto-generated?method?stub ?????????MyMediaController.stop.execute(); ?? ?????};? ?? ?}? 本篇是Android Service的思考(7)的完結篇,Service用起來不難,但是理解其中的機制就有一定的難度了,尤其是遠程調用一定要對遠程代理有深刻的認識才行。本系列設計的源碼很多,如果有人需要請加最牛網的群找傻蛋索要。
總結
以上是生活随笔為你收集整理的Android Service(7)--完结篇的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。