0
我綁定到服務,使用context.bindService(bindIntent, connection, Context.BIND_AUTO_CREATE);
。該方法返回true
。但我沒有聯繫。有些代碼:Android綁定到服務
// Snippet from method, that connects to service:
boolean result = context.bindService(bindIntent, connection, Context.BIND_AUTO_CREATE);
Log.d(TAG, "is connected: " + connected);
return result;
}
// Connection:
private ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
parser = Parser.Stub.asInterface(service);
Log.d(TAG, "Connected to service...");
connected = true;
}
public void onServiceDisconnected(ComponentName name) {
parser = null;
Log.d(TAG, "Disconnected from service...");
connected = false;
}
};
最終,我沒有進入onServiceConnected
方法:在logcat中沒有任何消息,除了D/FrameworkBridge( 926): is connected: false
。 任何想法?
UPD:我在連接到它後立即調用服務方法(即拋出NPE),並且一切正常:服務已啓動並綁定到該服務。似乎連接是在單獨的線程中創建的,等等。有什麼辦法可以讓我的課程等待,實際上建立連接嗎?
只是叫' context.startService(bindIntent);'在調用'bindService'方法之後。沒有幫助,同樣的問題。 – folone 2011-03-09 15:27:30
在上面的答案中添加了我使用的代碼。 – Christian 2011-03-09 19:02:39
對我的代碼進行了修改,使其與您的代碼類似。仍然沒有運氣。我還從類中調用'startService'和'bindService',這不是'Activity',也沒有它自己的'Context'。它是一個實用程序類,它接受'Context'作爲構造函數參數,然後使用它來啓動並綁定到服務。這可能是相關的嗎? – folone 2011-03-10 08:06:07