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 +