android - bindService的疑惑

标签 android android-activity android-service android-service-binding

我对 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/

相关文章:

java - 在 Android 中检测屏幕关闭/打开并更改 wifi 状态

android - 如何在应用程序关闭/最小化时停止 MediaPlayer 声音?

java - Android从视频/拍摄视频时获取特定帧

android - 安全移除 Android Activity

android - Web 浏览器应用程序 - 如何拦截 URL Intent ?

Android 致命信号 11 (SIGSEGV) 位于 0x636f7d89 (code=1)。如何追踪它?

java - 无法使物体绕圈移动

java - 在正则表达式中添加 2 个单词之间的范围

android - 命名扩展 Android Activity 类的类有哪些经验规则?

android - 如何停止android应用程序kill上的所有服务,线程,卸载库