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

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值