我想注册一个gcm令牌但是有例外。
我的android设备中的操作系统版本是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'
我不明白为什么会有这样的例外。
谢谢。