2016-04-06 26 views
0

我已經實現了類RegistrationIntentService,它擴展了IntentService並獲得onHandleIntent中的設備令牌。使用GCM推送通知 - 何時啓動IntentService?

我的問題是,我應該什麼時候開始這項服務?如果我在應用啓動時通過MainActivity開始,則不會調用onHandleIntent。什麼時候什麼時候開始意圖的正確方式?

回答

0

看看GitHub上的GCM's project MainActivty,很簡單&簡單。

具體而言,需要調用RegistrationIntentSerice如果checkPlayServices()是真的..

private boolean checkPlayServices() { 
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); 
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (apiAvailability.isUserResolvableError(resultCode)) { 
      apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) 
        .show(); 
     } else { 
      Log.i(TAG, "This device is not supported."); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 

聲明checkPlayServices()方法後,您需要檢查它是否是真實和運行服務:

if (checkPlayServices()) { 
     // Start IntentService to register this application with GCM. 
     Intent intent = new Intent(this, RegistrationIntentService.class); 
     startService(intent); 
    } 
+0

我的實現並沒有太大的不同。我從'MainActivity'調用'startService(intent)',但是'onHandleIntent'永遠不會被調用 –