现在的位置: 首页 > 综合 > 正文

Android ApiDemos示例解析(40):App->Service->Local Service Controller

2013年10月27日 ⁄ 综合 ⁄ 共 732字 ⁄ 字号 评论关闭

Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,LocalServiceActivities.Controller 启动Local Service 之后就基本上不管LocalService了

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会自动消失。

此外本例LocalService 之所以称为“Local” Service,除了上面提到的提供IBinder对象之间调用接口,只允许同一Application的组件来调用外。LocalService 在AndroidManifest.xml中定义为:

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

不含任何Intent-Filter,表示只能通过Explicit 指定Service类,不同应用一般来说无法直接访问这个类。

 

抱歉!评论已关闭.