总是在应用中为gcm:
获取“gcm服务不可用”演示活动背景注册过程的代码:
private void registerInBackground() {
new AsyncTask<Void, Void, String>(){
@Override
protected String doInBackground(Void... params) {
String msg="";
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
Log.d(TAG, "Attempt #" + i + " to register");
try{
if (gcm == null){
gcm = GoogleCloudMessaging.getInstance(context);
}
//Should get registrationid from gcm server
//but this portion is never running and control tranfers to
//catch portion with error "service not available "
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
Log.i(TAG,msg);
sendRegistrationIdToBackEnd();
storeRegistrationId(context, regid);
} catch(IOException e){
//here getting the error message service not available
msg = "Error: " + e.getMessage();
Log.e(TAG, "Failed to register on attempt " + i + ":" + e);
if (i == MAX_ATTEMPTS) {
break;
}
try {
Log.d(TAG, "Sleeping for " + backoff + " ms before retry");
Thread.sleep(backoff);
} catch (InterruptedException e1) {
// Activity finished before we complete - exit.
Log.d(TAG, "Thread interrupted: abort remaining retries!");
Thread.currentThread().interrupt();
return "";
}
// increase backoff exponentially
backoff *= 2;
}
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null,null,null);
}
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="14" />
<!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
<!-- The targetSdkVersion is optional, but it's always a good practice
to target higher versions. -->
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--
Creates a custom permission so only this app can receive its messages.
NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
where PACKAGE is the application's package name.
-->
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".DemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
</application>
</manifest>
完整的应用代码: https://drive.google.com/file/d/0B4rrYgKfVJpQSDAwWU8wNl96Ylk/edit?usp=sharing
答案 0 :(得分:0)
如果寄存器(或取消注册)操作是同步的,则可以在简单的循环中重试。但是,由于它是异步的,最好的方法是安排PendingIntent重试操作。