我有叫Otto Event
稱爲LoadAuthenticateEvent
那麼這個事件去一個活動我ClientManager.java
在下面的代碼是:合適的設計模式來抓取GCM推送通知的註冊ID?
@Subscribe
public void onLoadAuthenticateEvent(LoadAuthenticateEvent loadAuthenticateEvent) {
// GCM cannot register on the main thread
String deviceID = "";
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
String differentId = GCMRegistrationUtil.registerDevice(mContext);
Log.d(TAG, "Device Id: " + differentId);
}
});
thread.start();
String email = loadAuthenticateEvent.getEmail();
String password = loadAuthenticateEvent.getPassword();
Callback<User> callback = new Callback<User>() {
@Override
public void success(User user, Response response) {
sClient.setOrganization(user.getRole().getOrganization().getSubdomain());
mBus.post(new LoadedMeEvent(user));
}
@Override
public void failure(RetrofitError retrofitError) {
mBus.post(new LoadedErrorEvent(retrofitError));
}
};
sClient.authenticate(email, password, deviceID, PLATFORM, callback);
}
的問題是,服務器需要deviceID
,但GCM
需要一個電話是異步而不是在主線程上,我應該如何去實現這一點,我可以正確獲取deviceID並將其傳遞給sClient
?由於deviceID
可能爲空。
謝謝Koh我現在回到這個實現,現在看看這個。 – AndyRoid 2015-05-22 20:47:29