Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android ApiDemos示例解析(43):App->Service->Remote Service Controller

Android ApiDemos示例解析(43):App->Service->Remote Service Controller

編輯:Android開發教程

Remote Service Controller 和使用Local Service的Android ApiDemo示例解析(40):App->Service->Local Service Controller

都是使用Service的“Started” 模式,RemoteService在 AndroidManifest.xml中的定義如下:

<service android:name=”.app.RemoteService” android:process=”:remote”>

<intent- filter>

< !– These are the interfaces supported by the service, which

you can bind to. – >

<action android:name=”com.example.android.apis.app.IRemoteService” />

<action android:name= ”com.example.android.apis.app.ISecondary” />

< !– This is an action code you can use to select the service

without explicitly supplying the implementation class. –>

<action android:name=” com.example.android.apis.app.REMOTE_SERVICE” />

< /intent-filter>

< /service>

RemoteService.Controller 中啟動和停止Service的代碼如下,和“Bound” Service模式相比要簡單的多 :

// Make sure the service is started.  It will continue running     
// until someone calls stopService().
// We use an action code here, instead of explictly supplying
// the component name, so that other packages can replace
// the service.
startService(new Intent(
 "com.example.android.apis.app.REMOTE_SERVICE"));

... ...

 // Cancel a previous call to startService().  Note that the
// service will not actually stop at this point if there are
// still bound clients.
stopService(new Intent(
 "com.example.android.apis.app.REMOTE_SERVICE"));

“Started” Service在啟動之後會一直運行,直到調用stopService為止。因此在按“Start Service”後,在運行Android ApiDemo示例解析 (42):App->Service->Remote Service Binding 來綁定這個RemoteService,由於RemoteService已經啟動,你會發現例 42中的Received from Service的初始值一般不會為1。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved