幾乎遠程服務工作的各個實例包含這樣的代碼(這個是從谷歌IabHelper拍攝)處理遠程服務器斷開連接的正確方法是什麼?
mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
logDebug("Billing service disconnected.");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
logDebug("Billing service connected.");
mService = getServiceFromBinder(service);
...
}
};
爲什麼領域MSERVICE總是被設置爲null?忽略onServiceConnected回調是錯誤的嗎?根據我的經驗,重新連接通常會在1-2秒後發生。 儘管該字段被廣泛使用,但Google IABHelper甚至不會檢查mService是否爲null,即使是多個異步方法。很多我的用戶在斷開連接時都會得到NPE。 我想修補IabHelper。現在的問題是如何..
什麼是當現場MSERVICE是異步方法中用於處理disconneсtion有道?只需忽略onServiceDisconnected並獲得RemoteExceptions?我想過等待通知的方法,但不保證重新連接會發生。 歡迎任何想法。
謝謝你的回答。提到的問題是我自己的:) – akarimova
但在多線程環境中檢查null不是正確的方法。無論如何,謝謝。 – akarimova
ServiceConnection的文檔聲明:「像來自系統的許多回調一樣,此類上的方法是從您的進程的主線程調用的。」所以,在這種情況下,多線程執行的問題不適用。 ServiceConnection對象保持活動狀態,如果服務恢復時,它的onServiceConnected()方法將被調用,並且mService將被設置爲非null,並且您將準備好再次處理購買。 http://developer.android.com/reference/android/content/ServiceConnection。html – Carl