我有叫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