我可以从GCM服务器获取注册ID并捕获与gcm-alert Push Notification online tool一起发送的通知,但 GcmIntentService 的 onHandleIntent 方法永远不会被调用。<登记/>
有一件有趣的事情是调用 onDestroy 方法。任何帮助将不胜感激。
的 GcmIntentService.java
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>
答案 0 :(得分:1)
来自IntentService#onStartCommand(Intent intent, int flags, int startId)
的文档:
/** * You should not override this method for your IntentService. Instead, * override {@link #onHandleIntent}, which the system calls when the IntentService * receives a start request. * @see android.app.Service#onStartCommand */
我看到您在实施中覆盖onStartCommand()
,尝试坚持使用默认值。
答案 1 :(得分:0)
我看到它被调用但该方法中的断点永远不会起作用,因为 IntentService 在另一个线程中工作。
并且调用了 onDestroy ,因为从onHandleIntent()返回后,IntentService会自行停止。
感谢您的回答Service.onDestroy() is called directly after creation, anyway the Service does its work