Android Studio 3.2
public class GcmWakefulBroadcastReceiver extends WakefulBroadcastReceiver {
private static String TAG;
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService_.class.getName());
startWakefulService(context, (intent.setComponent(comp))); // crash here
setResultCode(Activity.RESULT_OK);
}
}
public class GcmIntentService extends IntentService {
private static String TAG = GcmIntentService.class.getName();
public GcmIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
}
}
在 Android 8.0-上成功运行。应用程序处于后台。
但是在 Android 8.0 + 上,应用程序因错误而崩溃:
FATAL EXCEPTION: main
Process: com.myproject.app, PID: 6506
java.lang.RuntimeException: Unable to start receiver com.myproject.app.gcm.GcmWakefulBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.myproject.app cmp=com.myproject.app/.gcm.GcmIntentService_ (has extras) }: app is in background uid UidRecord{27efe68 u0a164 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.myproject.app cmp=com.myproject.app/.gcm.GcmIntentService_ (has extras) }: app is in background uid UidRecord{27efe68 u0a164 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
at android.app.ContextImpl.startService(ContextImpl.java:1477)
at android.content.ContextWrapper.startService(ContextWrapper.java:650)
at android.content.ContextWrapper.startService(ContextWrapper.java:650)
at com.myproject.app.gcm.GcmWakefulBroadcastReceiver.onReceive(GcmWakefulBroadcastReceiver.java:44)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)
... 8 more
broadcast intent callback: result=CANCELLED
此行崩溃:
startWakefulService(context, (intent.setComponent(comp))); // crash here
我的应用必须在 Android 4.4 +
上运行答案 0 :(得分:0)
使用JobIntentService代替IntentService。
public class GcmIntentService extends JobIntentService {
private static String TAG = GcmIntentService.class.getName();
public static void startService(Context context) {
enqueueWork(context, GcmIntentService.class, 1001, new Intent());
}
@Override
protected void onHandleWork(Intent intent) {
}
}
然后从您的接收者那里:
public class GcmWakefulBroadcastReceiver extends WakefulBroadcastReceiver {
private static String TAG;
@Override
public void onReceive(Context context, Intent intent) {
GcmIntentService.startService(context);
}
}
如果仅将GcmWakefulBroadcastReceiver用于启动服务,则无需使用WakefulBroadcastReceiver。 在Android O上运行时,JobScheduler将负责唤醒 为您锁定(入队时持有唤醒锁 直到作业已分派并且正在运行。)
https://developer.android.com/reference/android/support/v4/app/JobIntentService