【正文】
()。 } } 建立 AIDL服務(wù)端 遠(yuǎn)程 Service的編寫和本地 Service很相似,只是遠(yuǎn)程 Service中 onBind()方法返回的 IBinder對(duì)象不同,該 Service代碼如下。 AIDL使用時(shí)需注意: 建立 AIDL文件 建立 AIDL服務(wù)端 遠(yuǎn)程 Service的編寫和本地 Service很相似,只是遠(yuǎn)程 Service中 onBind()方法返回的 IBinder對(duì)象不同,該 Service代碼如下。由于它實(shí)現(xiàn)了IBinder接口,因此可作為 Service的 onBind()方法的返回值。當(dāng)客戶端獲取了遠(yuǎn)程的 Service的 IBinder對(duì)象的代理之后,接下來可通過該 IBinder對(duì)象去回調(diào)遠(yuǎn)程 Service的屬性或方法。 ? 多次單擊綁定服務(wù)按鈕, 并不會(huì)重復(fù)執(zhí)行綁定方法 。 控制臺(tái)信息如下圖所示。 案例總結(jié) 通過 Context的 startService() 方法啟動(dòng)和停止 Service 通過 Context的 bindService() 方法綁定和解除綁定 兩種方式混合使用 s t a r t S e r v i c es t o p S e r v i c e按 鈕s t a r t S e r v i c e . s e t O nC l i c k L i s t e n e r ( ) 方法 調(diào) 用 s t a r t S e r v i c e( ) 方 法 。 private ServiceConnection conn=new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { (TAG,MainActivity onServiceDisconnected invoked!)。 ()。 public class MyBinder extends Binder { public MyBinder() { (TAG, MyBinder Constructure invoked!)。 (綁定 Service案例) 當(dāng)單擊綁定 Service按鈕后 , 再重復(fù)多次單擊綁定 Service按鈕 , 查看控制臺(tái)打印信息 , 發(fā)現(xiàn) 程序并不會(huì)多次調(diào)用 onBind()方法 。 (new OnClickListener() { public void onClick(View v) { bindService(intent, conn,)。 (intent)。 private ServiceConnection conn=new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { (TAG,MainActivity onServiceDisconnected invoked!)。 綁定 Service過程 Context的 bindService()方法的完整方法簽名為: bindService( Intent service,ServiceConnection conn, int flags), 該方法的三個(gè)參數(shù)解釋如下: ?service: 該參數(shù)表示與服務(wù)類相關(guān)聯(lián)的 Intent對(duì)象,用于指定所綁定的 Service應(yīng)該符合哪些條件; ?conn: 該參數(shù)是一個(gè) ServiceConnection對(duì)象,該對(duì)象用于監(jiān)聽訪問者與 Service之間的連接情況。 } })。 如果想停止服務(wù) , 需要顯示地調(diào)用 stopService()方法 , 下面代碼中 , 使用 Activity作為 Service的啟動(dòng)者 ,分別定義了啟動(dòng) Service和關(guān)閉 Service兩個(gè)按鈕 , 并為它們添加了事件處理 。 ()。 Service中常用方法簡介 案例 :FirstService 定義的 Service子類必須實(shí)現(xiàn) onBind()方法 , 然后還需在 文件中對(duì)該 Service子類進(jìn)行配置 , 配置時(shí)可通過 intentfilter...元素指定它可被哪些 Intent啟動(dòng) 。通過使用獨(dú)立的線程,你就會(huì)降低程序出現(xiàn) ANR( Application No Response程序沒有響應(yīng))的風(fēng)險(xiǎn),程序的主線程仍然可以保持與用戶的交互。 啟動(dòng) Service的兩種方式 在 Android系統(tǒng)中,常采用以下兩種方式啟動(dòng) Service. (1)通過 Context的 startService( ) 啟動(dòng) service后 , 訪問者與 service之間沒有關(guān)聯(lián) , 該 service將一直在后臺(tái)執(zhí)行 , 即使調(diào)用 startservice的進(jìn)程結(jié)束了 , service仍然還存在 , 直到有進(jìn)程調(diào)用 stopSer v ice( ) , 或者 s erv ice自己停止( stopSelf) 。 (2)通過 Context的 bindService( ) 綁定 Service, 綁定后 Service就和調(diào)用bindService的組件同生共死了 。 ?開發(fā) Service組件需要先開發(fā)一個(gè) Service子類 ,然后在 配置該 Service,配置時(shí)可通過intentfilter…/ 元素指定它可被哪些 Intent啟動(dòng)。 public IBinder onBind(Intent arg0) { (TAG, MyService onBind invoked!)。 return (intent, flags, startId)。 start=(Button)findViewById()。 } } 案例 :FirstService 案例 :FirstService 運(yùn)行本節(jié)的例子后,第一次單擊【啟動(dòng) Service】按鈕后,在 DDMS視圖下的LogCat視圖有如下圖所示的輸出。該參數(shù)可指定 BIND_AUTO_CREATE(自動(dòng)創(chuàng)建 )。 } }。 setContentView()。 } })。 修改我們的 MyService類 , 添加一個(gè)內(nèi)部類MyBinder, 同時(shí)在 onCreate()方法中啟動(dòng)一個(gè)線程 , 模擬后臺(tái)服務(wù) , 該線程主要是做數(shù)據(jù)遞增操作 , 在 MyBinder類中 , 提供一個(gè)方法 , 可以獲取當(dāng)前遞增的值 ( count的值 ) , 具體代碼如下 。 return myBinder。 } public void onDestroy() { (TAG, MyService onDestroy invoked!)。 } }。按 鈕s t o p S e r v i c e . s e t O n C l i c kL i s t e n e r ( ) 方 法 調(diào) 用s t o p S e r v i c e ( ) 方 法 。 總結(jié): 調(diào)用順序如下: onCreate()→ onBind()→ onServiceConnected() → [onStartCommand() 1到 N次 ]→ onUnBind[→ onServiceConnected() → onRebind()0到 N次 → onUnBind]→onDestroy() 。 ?進(jìn)程之間的通信信息,首先會(huì)被轉(zhuǎn)換成 AIDL協(xié)議消息 ,然后發(fā)送給對(duì)方,對(duì)方收到 AIDL協(xié)議消息后再 轉(zhuǎn)換成相應(yīng)的對(duì)象 。 如果使用自定義類型作為參數(shù)或返回值 , 自定義類型必須實(shí)現(xiàn) Parcelable接口 。 String getAuthor()。 private String name,author。 (new TimerTask() { public void run() { int rand=(int)(()*3)。因此,與本地 Service相比,開發(fā)遠(yuǎn)程 Service要多定義一個(gè) AIDL接口。 public void onCreate(Bundle savedInstanceState) { (savedInstanceState)。 (new OnClickListener() { public void onClick(View v) { try{ (())。 unbindService(conn)。 程序清單: /SendMessage/res/layout/ public class MainActivity extends Activity { EditText num, mess。 String content=().toString()。 } }