【发布时间】:2016-11-28 08:19:33
【问题描述】:
包nath.prem.com.premgcmproject;
导入android.content.Intent;
导入 com.google.android.gms.iid.InstanceIDListenerService;
/** * 由 prem 于 22/7/16 创建。 */ 公共类 GCMTokenRefreshListenerService 扩展 InstanceIDListenerService {
//如果token改变了再次注册设备 @Override public void onTokenRefresh() { Intent intent = new Intent(this, GCMRegistrationIntentService.class);启动服务(意图); } }
在客户端获取 GCM 令牌时出错
致命异常:IntentService[]
java.lang.IncompatibleClassChangeError: com.google.android.gms.iid.zzd.zzdL(未知来源)的 android.support.v4.content.ContextCompat
在
com.google.android.gms.iid.zzd.(Unknown Source) at com.google.android.gms.iid.zzd.(Unknown Source) at com.google.android.gms.iid.InstanceID.zza (未知来源)在 com.google.android.gms.iid.InstanceID.getInstance(未知来源)
这是我在获取 gcm 令牌时遇到的错误
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) { //注册gcm到设备 registerGCM(); } private void registerGCM() { //注册完成意图最初 null Intent registrationComplete = null; //注册token也为null //注册成功就会得到token String token = null; SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); try { //创建一个instanceid InstanceID instanceID = InstanceID.getInstance(this); // // //从实例id中获取token token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); //在日志中显示令牌,以便我们可以复制它以发送推送通知 //您还可以通过将令牌存储到您的服务器来扩展应用程序 Log.w("GCMRegIntentService", "token:" + token); //注册完成创建意图成功registrationComplete = new Intent(REGISTRATION_SUCCESS); //将token放入intent // registrationComplete.putExtra("token", token); } catch (Exception e) { //如果发生任何错误 Log.w("GCMRegIntentService", "Registration error");注册完成 = 新意图(注册错误); } //发送注册完成的广播 LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); } }
这是该类的 java 代码
这是我在项目中的清单文件......但是我在 RegistrationIntentService 中看不到任何日志:
【问题讨论】:
-
请编辑和格式化您的代码。检查这个stackoverflow.com/editing-help
标签: android android-intentservice