处理回退,而在Android的实施C2DM [英] Handling backoff while implementing C2DM in Android

查看:173
本文介绍了处理回退,而在Android的实施C2DM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现在应用中的一个C2DM。如果有一个错误,而登记的C2DM我们需要退避,然后再试一次。我想用户是否需要去用相同的注册意图或是否有任何其他意图的用户需要调用重试的目的。

I am implementing C2DM in one of the application. In case there is an error while registering for C2DM we need to backoff and then try again. I want to whether the user needs to go with the same registration intent or is there any other intent that user needs to call for retry purpose.

下面是我在code一看到这两个意图。但如果注册失败重试实际上从未发生,我没有看到正在生成的注册ID。

Below are the two intents that I have seen in one of the code. But in case the registration fails the retry actually never occurs and I do not see any registration id being generated.

com.google.android.c2dm.intent.REGISTER
com.google.android.c2dm.intent.RETRY

com.google.android.c2dm.intent.REGISTER com.google.android.c2dm.intent.RETRY

http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html 。这是code,它在应用程序中使用。

http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html. This is the code that is being used in the app.

请指教至于怎么做,我们需要处理的后退。

Please advice as to how do we need to handle the back off.

推荐答案

您需要设置闹钟一回了一段时间后重新尝试注册
您需要重试的意图过滤器添加到在manifest.html接收申报和报警code可能看起来像这样

You need to set an alarm to retry the registration after a back off period of time You need to add the RETRY intent filter to the receiver declaration in the manifest.html and the alarm code could look something like this

    if ("SERVICE_NOT_AVAILABLE".equals(error)) {
        long backoffTimeMs = C2DMessaging.getBackoff(context);

        Log.d(TAG, "Scheduling registration retry, backoff = " + backoffTimeMs);
        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, SystemClock.elapsedRealtime() + backoffTimeMs,
                retryPIntent);

        // Next retry should wait longer.
        backoffTimeMs *= 2;
        C2DMessaging.setBackoff(context, backoffTimeMs);
    } 

C2DMessaging距离jumpnote应用类可以在这里的http找到:/ /$c$c.google.com/p/jumpnote/source/checkout

更新

确认重试权限在manifest文件设置为我在您的评论回应建议。我的清单看起来像这样

Make sure the RETRY permission is set in your manifest file as suggested in my response to your comment. My manifest looks something like this

    <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <!-- Receive the actual message -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="my.package.name" />
        </intent-filter>
        <!-- Receive the registration id -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="my.package.name" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver">
        <intent-filter>
         <action android:name="com.google.android.c2dm.intent.RETRY"/>
         <category android:name="io.modem" />
    </intent-filter>
    </receiver>

此外 - 这是很容易测试在仿真器 - 只需断开P.C.从互联网上,你会得到重试服务不可用异常事件,直到您重新连接到互联网。

Also - This is easy to test on the emulator - Just disconnect your P.C. from the internet and you will get retry events for service not available exceptions until you re-connect to the internet

这篇关于处理回退,而在Android的实施C2DM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆