Android 8.0 Java.lang.IllegalStateException:不允许启动服务意图



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) {
    }
}

on 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

使用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来启动服务,那么您无需使用wawefulbroadcastreceiver。 在Android o上跑步时,JobsCheduler将负责Wake 为您锁定(从您加入工作的时候握住尾锁 直到派遣工作并在运行时)。

https://developer.android.com/reference/android/support/v4/app/jobintentservice

最新更新