2016-05-23 32 views
0

從官方website,我下載GCM的一個例子,有一個可以檢測刷新令牌服務,代碼如下:GCM:我應該在Android中自己啓動InstanceIDListenerService嗎?

public class MyInstanceIDListenerService extends InstanceIDListenerService { 

private static final String TAG = "MyInstanceIDLS"; 

/** 
* Called if InstanceID token is updated. This may occur if the security of 
* the previous token had been compromised. This call is initiated by the 
* InstanceID provider. 
*/ 
// [START refresh_token] 
@Override 
public void onTokenRefresh() { 
    // Fetch updated Instance ID token and notify our app's server of any changes (if applicable). 
    Intent intent = new Intent(this, RegistrationIntentService.class); 
    startService(intent); 
} 
// [END refresh_token] 
} 

我的問題是:我應該寫代碼來啓動自己服務? 就像:

startService(new Intent(getActivity(), MyInstanceIDListenerService.class)); 

回答

1

不可以。此服務在必要時由Google Play服務啓動。只要確保在你的AndroidManifest.xml文件中定義服務。另外,如果你剛開始,你可能想看看更容易實施FCM

查看FCM here的樣本。

+0

謝謝,你能給我更多的細節,Google Play服務,如何啓動它(哪個類啓動它)?就像「gcmListenerService」由「com.google.android.gms.gcm.GcmReceiver」啓動,你能告訴我InstanceIDListenerService是如何啓動的嗎? – fhlkm

+0

Google Play服務始終在運行。 InstanceIDListenerService也將由GcmReceiver啓動。 –

+0

如果sendRegistrationIdTo服務器調用失敗,該怎麼辦? – Javanator

相關問題