【起航計劃 035】2015 起航計劃 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller

Local Service Controller 是將LocalService看成「Started」Service來使用,相對於」Bound」 Service 來講,這種模式用法要簡單得多,LocalServiceActivities.Controller 啓動Local Service 以後就基本上無論LocalService了android

startService(new Intent(Controller.this,LocalService.class));

 LocalService能夠選擇本身中止本身,本例是經過調用stopService來中止Service ,若是你在本例中啓動Service以後,沒有stopService ,這個Service將在後臺一直運行,即便你退出AppDemo或是啓動其它應用(屏幕上方會一直顯示一個三角)。而對於Local Service Binding來講,狀況就不一樣,綁定Service以後,即便你沒有點擊「unbind Service 」按鈕,按「Back」鍵,Binding Activity退出時,Android系統會自動斷開與LocalService的綁定。屏幕上方Notification會自動消失。app

此外本例LocalService 之因此稱爲「Local」 Service,除了上面提到的提供IBinder對象之間調用接口,只容許同一Application的組件來調用外。LocalService 在AndroidManifest.xml中定義爲:this

<service android:name=」.app.LocalService」 />

 不含任何Intent-Filter,表示只能經過Explicit 指定Service類,不一樣應用通常來講沒法直接訪問這個類。spa

 

相關文章
相關標籤/搜索