在文檔的谷歌雲消息,谷歌說,「不要叫這種方法在主線程,而是使用擴展IntentService服務」爲什麼我應該使用IntentService獲取Google雲消息傳遞令牌?
以前,當我使用gcm.register(現deprecated) ,我把我的代碼放在aSyncTask中。
private void getRegId(final ArrayList<String> studentIds){
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String RegID;
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
}
RegID = gcm.register(getVal("GCMProject"));
} catch (IOException ex) {
RegID = "," + ex.getMessage();
}
return RegID;
}
@Override
protected void onPostExecute(final String RegID) {
String action = "SaveGCMRegID";
if (RegID.charAt(0) == ',') { //Error Message
action = "GCM Reg Error";
} else {
KVDB.StoreValue("GCMRegID", RegID);
}
}
}.execute(null, null, null);
}
IntentService代替了什麼?我明白不會在主線程中等待響應,但是一個SyncTask應該做到這一點,對吧? (我相信這個問題與Is there any reason to continue using IntentService for handling GCM messages?不同,後者側重於使主線脫離工作)