2011-03-04 80 views
2

我使用C2DM,如果注册成功,它工作正常。但有时注册失败,然后尝试以后注册:C2DM注册重试

Intent retryIntent = new Intent(C2DM_RETRY); 
PendingIntent retryPIntent = PendingIntent.getBroadcast(context, 
     0 /*requestCode*/, retryIntent, 0 /*flags*/); 

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
am.set(AlarmManager.ELAPSED_REALTIME, 
      backoffTimeMs, retryPIntent); 

但是,如果警报管理器触发此意图该怎么办?我必须抓住它吗?因为不知何故程序从未重新注册。

回答

5

首先。提供的重试代码是错误!是的,即使谷歌的人可以发布错误的代码!

am.set方法(在C2DMBaseReceiver.handleRegistration中)接受启动后的时间,以毫秒为单位应该触发意图。我们通过了30000,60000,120000等等。所有这些值在过去都会很好。我们应该传递的是:

am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + backoffTimeMs, 
         retryPIntent); 

这意味着我们要说的下一个目标应该在现在+ backOffTimeMs被解雇。这是已发布代码中的第一个错误。

第二个错误是,没有任何广播接收器被接线以接收

com.google.android.c2dm.intent.RETRY

意图!

所以,我们包括清单文件作如下补充:

<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"> 
    <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RETRY"/> 
      <category android:name="com.google.android.apps.chrometophone" /> 
      </intent-filter> 
</receiver> 

(这是一个额外的块,保留所有其他事情是)

而且你去那里!它将开始工作!

+0

感谢您的回答。它帮助了我很多,但我得到一个错误Permission Denial:从my.package.name(pid = -1,广播意图){act = com.google.android.c2dm.intent.RETRY flg = 0x4(有额外) uid = 10041)需要com.google.android.c2dm.permission.SEND,因为接收者my.package.name/com.google.android.c2dm.C2DMBroadcastReceiver - 如果您可以扩展您的答案以包含所需内容为解决这个问题提供一个完整的答案。再次感谢 – jamesc 2011-10-13 16:01:19