Android Studio中的GCM

时间:2016-06-30 00:10:51

标签: android notifications google-cloud-messaging

我尝试使用Android Studio在Android应用中使用谷歌通知,我有这个类到注册服务

public class GCMRegistrationIntentService extends IntentService
{

public static final String REGISTRATION_SUCCESS = "RegistrationSuccess";
public static final String REGISTRATION_ERROR = "RegistrationError";

public GCMRegistrationIntentService() {
    super("");
}


@Override
protected void onHandleIntent(Intent intent) {
    registerGCM();
}

private void registerGCM() {
    Intent registrationComplete = null;
    String token = null;
    try {
        InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
        token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        Log.w("GCMRegIntentService", "token:" + token);
        //notify to UI that registration complete success
        registrationComplete = new Intent(REGISTRATION_SUCCESS);
        registrationComplete.putExtra("token", token);
    } catch (Exception e) {
        Log.w("GCMRegIntentService", "Registration error");
        registrationComplete = new Intent(REGISTRATION_ERROR);
    }
    //Send broadcast
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}

}

但是当我尝试调用方法registerGCM()时它会显示我的错误

06-29 18:00:39.611 5494-5539/com.cisaApp.adrian.cisaappacueductos E/AndroidRuntime: FATAL EXCEPTION: IntentService[]
                                                                                Process: com.cisaApp.adrian.cisaappacueductos, PID: 5494
                                                                                java.lang.IncompatibleClassChangeError: The method 'java.io.File android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)' was expected to be of type virtual but instead was found to be of type direct (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar)
                                                                                    at com.google.android.gms.iid.zzd.zzdL(Unknown Source)
                                                                                    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
                                                                                    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
                                                                                    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
                                                                                    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
                                                                                    at com.cisaApp.adrian.cisaappacueductos.GCMRegistrationIntentService.registerGCM(GCMRegistrationIntentService.java:34)
                                                                                    at com.cisaApp.adrian.cisaappacueductos.GCMRegistrationIntentService.onHandleIntent(GCMRegistrationIntentService.java:27)
                                                                                    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.os.HandlerThread.run(HandlerThread.java:61)

特别是

InstanceID instanceID = InstanceID.getInstance(getApplicationContext());

1 个答案:

答案 0 :(得分:0)

对于您的错误IncompatibleClassChangeError,根据此thread,这意味着您已对库进行了一些不兼容的二进制更改,而无需重新编译客户端代码。有关此问题的更多信息,请阅读上面的链接。

因此,对于问题的解决方案,您可以按照此相关SO question中的解决方案进行操作。尝试使用gradle依赖树来解决此错误。

运行gradle -q app:dependencies --configuration compile并检查输出以查找以下条目:

+--- com.mcxiaoke.viewpagerindicator:library:2.4.1
|    \--- com.android.support:support-v4:+ -> 24.0.0-beta1 (*)

同时将您的依赖项更新为最新版本以解决此问题。