Service官方教程(9)绑定服务时的注意事项
Binding to a Service
Application components (clients) can bind to a service by calling bindService()
. The Android system then calls the service's onBind()
method, which returns an IBinder
for interacting with the service.
The binding is asynchronous. bindService()
returns immediately and does not return the IBinder
to the client. To receive the IBinder
, the client must create an instance of ServiceConnection
and pass it to bindService()
. The ServiceConnection
includes a callback method that the system calls to deliver the IBinder
.
Note: Only activities, services, and content providers can bind to a service—you cannot bind to a service from a broadcast receiver.
So, to bind to a service from your client, you must:
- Implement
ServiceConnection
.Your implementation must override two callback methods:
onServiceConnected()
- The system calls this to deliver the
IBinder
returned by the service'sonBind()
method. onServiceDisconnected()
- The Android system calls this when the connection to the service is unexpectedly lost, such as when the service has crashed or has been killed. This is not called when the client unbinds.
- Call
bindService()
, passing theServiceConnection
implementation. - When the system calls your
onServiceConnected()
callback method, you can begin making calls to the service, using the methods defined by the interface. - To disconnect from the service, call
unbindService()
.If your client is still bound to a service when your app destroys the client, destruction causes the client to unbind. It is better practice to unbind the client as soon as it is done interacting with the service. Doing so allows the idle service to shut down. For more information about appropriate times to bind and unbind, see Additional Notes.
For example, the following snippet connects the client to the service created above by extending the Binder class, so all it must do is cast the returned IBinder
to the LocalService
class and request the LocalService
instance:
LocalService mService;
private ServiceConnection mConnection = new ServiceConnection() {
// Called when the connection with the service is established
public void onServiceConnected(ComponentName className, IBinder service) {
// Because we have bound to an explicit
// service that is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
} // Called when the connection with the service disconnects unexpectedly
public void onServiceDisconnected(ComponentName className) {
Log.e(TAG, "onServiceDisconnected");
mBound = false;
}
};
With this ServiceConnection
, the client can bind to a service by passing it to bindService()
. For example:
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
- The first parameter of
bindService()
is anIntent
that explicitly names the service to bind (thought the intent could be implicit). - The second parameter is the
ServiceConnection
object. - The third parameter is a flag indicating options for the binding. It should usually be
BIND_AUTO_CREATE
in order to create the service if its not already alive. Other possible values areBIND_DEBUG_UNBIND
andBIND_NOT_FOREGROUND
, or0
for none.
Additional notes 注意事项
Here are some important notes about binding to a service:
- You should always trap
DeadObjectException
exceptions, which are thrown when the connection has broken. This is the only exception thrown by remote methods. - Objects are reference counted across processes.
进程间对象是 引用+引用计数 访问。
- You should usually pair the binding and unbinding during matching bring-up and tear-down moments of the client's lifecycle. For example:
- If you only need to interact with the service while your activity is visible, you should bind during
onStart()
and unbind duringonStop()
. - If you want your activity to receive responses even while it is stopped in the background, then you can bind during
onCreate()
and unbind duringonDestroy()
. Beware that this implies that your activity needs to use the service the entire time it's running (even in the background), so if the service is in another process, then you increase the weight of the process and it becomes more likely that the system will kill it.绑定与解绑成对出现,注意组件的生命周期。通常可以在onCreate中绑定,onDestory解绑.而不要在onResume,onPause中绑定,解绑。
Note: You should usually not bind and unbind during your activity's
onResume()
andonPause()
, because these callbacks occur at every lifecycle transition and you should keep the processing that occurs at these transitions to a minimum. Also, if multiple activities in your application bind to the same service and there is a transition between two of those activities, the service may be destroyed and recreated as the current activity unbinds (during pause) before the next one binds (during resume). (This activity transition for how activities coordinate their lifecycles is described in the Activities document.) - If you only need to interact with the service while your activity is visible, you should bind during
For more sample code, showing how to bind to a service, see the RemoteService.java
class in ApiDemos.
最新文章
- 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景
- 二维码跳转不同的 app store
- php抽奖代码
- Linq和Lamda表达式的简单处理方式
- CSS3的新属性的一下总结
- Tomcat中部署WEB项目的四种方法
- c语言数组不同初始化方式的结果
- BOT、BT、PPP形式介绍(3)
- webapi 发布接口报405错误(angularjs2.0)
- 解决xshell评估期已过的问题
- EBS R12安装升级(FRESH)(三)
- 关于ES6
- 配置php环境的一个nginx.conf
- ORA-20011 ORA-29913 KUP-11024 GATHER_TABLE_STATS
- [爬虫]采用Go语言爬取天猫商品页面
- 获取验证码随机字符串@return string $captcha,随机验证码文字
- SPSS学习系列之SPSS Modeler怎么修改默认的内存大小(图文详解)
- Linux下的wine生活(QQ/微信/Office)
- 【laravel5.4】php artisan migrate报错:Specified key was too long; max key length is 767 bytes
- 修改oracle表空间数值