我想註冊一個gcm令牌但得到異常。註冊gcm令牌時應用程序崩潰(ExceptionInInitializerError)
我的android設備的os版本是android 5.0。
這是我的代碼註冊gcm令牌。
public static String refreshRegistationId(final Context context) {
String token = "";
try {
// [START register_for_gcm]
// Initially this call goes out to the network to retrieve the token, subsequent calls
// are local.
// [START get_token]
InstanceID instanceID = InstanceID.getInstance(context);
// R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
// See https://developers.google.com/cloud-messaging/android/start for details on this file.
token = instanceID.getToken(SENDER_ID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// [END get_token]
Log.i(TAG, "GCM Registration Token: " + token);
NidusTool.putStringSharedPreference(context, GCM_REGISTER_TOKEN, token);
// TODO: Implement this method to send any registration to your app's servers.
// sendRegistrationToServer(token);
// Subscribe to topic channels
// subscribeTopics(token);
// [END register_for_gcm]
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
// Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
// LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
return token;
}
ExceptionInInitializerError
調用instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE, null)
時,堆棧跟蹤
java.lang.ExceptionInInitializerError
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:306)
at java.security.Provider$Service.newInstance(Provider.java:1072)
at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
at java.security.Signature.tryAlgorithmWithProvider(Signature.java:217)
at java.security.Signature.tryAlgorithm(Signature.java:203)
at java.security.Signature.getSignature(Signature.java:175)
at java.security.Signature.getInstance(Signature.java:105)
at com.google.android.gms.iid.zzc.zza(Unknown Source)
at com.google.android.gms.iid.zzc.zza(Unknown Source)
at com.google.android.gms.iid.zzc.zzb(Unknown Source)
at com.google.android.gms.iid.zzc.zza(Unknown Source)
at com.google.android.gms.iid.InstanceID.zzc(Unknown Source)
at com.google.android.gms.iid.InstanceID.getToken(Unknown Source)
這是我的gradle中出現的依賴。
// for gcm to use support-annotations 23.0.1
compile 'com.android.support:support-annotations:23.0.1'
compile 'com.google.android.gms:play-services-gcm:8.1.0'
compile 'com.android.support:appcompat-v7:23.0.0'
我不明白爲什麼我會得到這樣的例外。
謝謝。
你有檢查你的清單和使用Internet權限? –
您可以針對Play Services 8.3編譯com.google.android.gms:play-services-gcm:8.3.0''進行編譯並查看是否已解決? – KayAnn
嗨,這個問題隨機發生。有時它可以正常工作,但有時會失敗並拋出異常。 –