我對Android綁定服務有些疑惑。該指南:http://developer.android.com/guide/components/bound-services.html ,約bindService()
,說:對bindService的疑問
The `bindService()` method returns immediately without a value
但這並不似乎是正確的,因爲here該方法的簽名是
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.
所以問題是:爲什麼文檔說方法returns immediately without a value
?此外,here,綁定在這樣做的話:
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);
}
我錯了嗎?
返回值有什麼意義恕我直言,例如,如果您的服務尚未運行,並綁定到它的標誌== 0返回值是true ,爲什麼?我不知道... – pskink 2014-09-30 18:05:09
是的,這是矛盾的,因爲我們可以知道連接是否被創建,只有當onServiceConnect()被調用時,而不是立即。 – GVillani82 2014-09-30 18:08:22
更糟糕的是:你的bindService()返回true,但是onServiceConnected()永遠不會被調用(當服務沒有啓動/創建並且標誌== 0時),我想你可以簡單地忘記返回的值並且只依賴於ServiceConnection – pskink 2014-09-30 18:12:24