我对 Android 绑定(bind)服务有一些疑问。导游:http://developer.android.com/guide/components/bound-services.html
,关于 bindService()
,说:
The `bindService()` method returns immediately without a value
但这似乎不正确,因为 here该方法的签名是
public abstract boolean bindService (Intent service, ServiceConnection conn, int flags)
其中返回的 bool 值说明如下:
If you have successfully bound to the service, true is returned; false is returned if the connection is not made so you will not receive the service object.
所以问题是:为什么文档说方法立即返回而没有值
?此外,here ,绑定(bind)是这样完成的:
void doBindService() {
bindService(new Intent(Binding.this,
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
而且我不明白mIsBound = true
的意义,因为 javadoc 说 bindService() 也可以返回 false,如果服务绑定(bind)失败。所以应该是:
void doBindService() {
mIsBound = bindService(new Intent(Binding.this,
LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
}
我错了吗?
最佳答案
文档不正确。当返回的 bool 值为 false 时,这意味着不再尝试建立连接。当返回 true 时,这意味着系统尝试建立连接,这可能成功也可能失败。
查看此问题的答案:"in what case does bindservice return false" . 基本上,当 bindservice 找不到要尝试绑定(bind)的服务时,它会返回 false。
关于android - bindService的疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26127129/