我可以從GCM服務器獲得註冊ID,趕上了與gcm-alert Push Notification online tool發送通知,但GcmIntentService不會被調用的onHandleIntent方法。
有一個有趣的事情,onDestroy方法被調用。任何幫助將不勝感激。
GcmIntentService.java
的Android onHandleIntent從未被稱爲
package ondermerol.com.studiopushtest;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.*;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p/>
* TODO: Customize class - update intent actions, extra parameters and static
* helper methods.
*/
public class GcmIntentService extends IntentService {
private final static String Tag="---IntentServicetest";
public GcmIntentService() {
// TODO Auto-generated constructor stub
super("GcmIntentService");
Log.d(Tag, "Constructor");
}
@Override
public void onDestroy() {
Log.d(Tag, "onDestroy()");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(Tag, "onStart()");
super.onStart(intent, startId);
}
@Override
public void setIntentRedelivery(boolean enabled) {
Log.d(Tag, "setIntentRedelivery()");
super.setIntentRedelivery(enabled);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.d(Tag, "IntentServicetest is onHandleIntent!");
}
GcmBroadcastReceiver.java
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
public GcmBroadcastReceiver() {
}
// Receives the broadcast directly from GCM service
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
ondermerol.com.studiopushtest.GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ondermerol.com.studiopushtest" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="ondermerol.com.studiopushtest.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="ondermerol.com.studiopushtest.permission.C2D_MESSAGE" />
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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.RETRY" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="ondermerol.com.studiopushtest" />
</intent-filter>
</receiver>
<service android:enabled="true"
android:name="ondermerol.com.studiopushtest.GcmIntentService">
</service>
</application>
</manifest>
如果需要,我可以給其他兩個類。 – ondermerol 2015-03-31 07:12:05
你怎麼知道它不叫? – 2015-03-31 07:12:34
我在GcmIntentService的evey方法上放置了斷點,並且看到它沒有被調用,儘管它的其他方法被調用。 – ondermerol 2015-03-31 07:13:57