Android 对服务的怀疑

Android 对服务的怀疑,android,android-activity,android-service,android-service-binding,Android,Android Activity,Android Service,Android Service Binding,我有一些关于Android绑定服务的建议。指南: ,关于bindService(),说: The `bindService()` method returns immediately without a value 但这似乎并不正确,因为该方法的签名是 public abstract boolean bindService (Intent service, ServiceConnection conn, int flags) 其中返回的布尔值描述如下: If you have success

我有一些关于Android绑定服务的建议。指南: ,关于
bindService()
,说:

The `bindService()` method returns immediately without a value
但这似乎并不正确,因为该方法的签名是

public abstract boolean bindService (Intent service, ServiceConnection conn, int flags)
其中返回的布尔值描述如下:

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.
所以问题是:为什么文档中说方法
立即返回而没有值?此外,绑定是通过以下方式完成的:

void doBindService() {
    bindService(new Intent(Binding.this, 
            LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}
我不理解
mIsBound=true
的含义,因为javadoc说如果绑定到服务失败,bindService()也可以返回false。因此,它应该是:

void doBindService() {
    mIsBound = bindService(new Intent(Binding.this, 
            LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
}

我错了吗?

文档不正确。当返回的布尔值为false时,这意味着不再尝试建立连接。当返回true时,这意味着系统尝试建立连接,这可能成功,也可能失败

看看这个问题的答案:。
基本上,bindservice在找不到甚至尝试绑定到的服务时返回false。

好的,我终于完成了android中绑定服务的所有细微差别的学习,这就是ServiceBindHelper类,它可以被视为“终极真理”(请原谅我的不诚实)

用法示例:

class MyActivity extends Activity {
    private ServiceBindHelper<MyService> myServiceHelper;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myServiceHelper = new ServiceBindHelper<MyService>(this) {
            @Override
            protected Intent createBindIntent() {
                return new Intent(MyActivity.this, MyService.class);
            }

            @Override
            protected MyService onServiceConnected(ComponentName name, IBinder service) {
                // assume, that MyService is just a simple local service
                return (MyService) service;
            }
        };
        myServiceHelper.bind();
    }

    protected void onDestroy() {
        super.onDestroy();
        myServiceHelper.unbind();
    }

    protected void onStart() {
        super.onStart();
        if (myServiceHelper.getService() != null) {
            myServiceHelper.getService().doSmth();
        }
    }
}
类MyActivity扩展活动{
私人服务BindHelper myServiceHelper;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
myServiceHelper=新ServiceBindHelper(此){
@凌驾
受保护的意图createBindIntent(){
返回新的意图(MyActivity.this、MyService.class);
}
@凌驾
受保护的MyService onServiceConnected(组件名称,IBinder服务){
//假设MyService只是一个简单的本地服务
返回(MyService)服务;
}
};
myServiceHelper.bind();
}
受保护的空onDestroy(){
super.ondestory();
myServiceHelper.unbind();
}
受保护的void onStart(){
super.onStart();
if(myServiceHelper.getService()!=null){
myServiceHelper.getService().doSmth();
}
}
}

返回值几乎没有意义,例如,如果您的服务尚未运行,并且您使用标志==0绑定到它,则返回值为true,为什么?我不知道…是的,这是矛盾的,因为只有在调用OnServiceConnection()时,而不是立即调用时,我们才能知道连接是否已创建。更糟糕的是:您的bindService()返回true,但从未调用OnServiceConnection()(服务未启动/创建且标志==0的情况),我认为您可以简单地忽略返回的值,只依赖ServiceConnectionOnlyRight。事实上,我的想法是在onServiceConnected()中设置一个错误绑定,让它默认为false,因此如果从未调用onServiceConnected(),变量将保持false。关于忽略返回的布尔值,我支持@pskink。我几乎感觉他们意外地把布尔返回放在那里,打算遵循文档中提到的无效约定。正如pskink所提到的,布尔值充其量也可能是误导性的