不允许启动服务 Intent - Android Oreo

Not allowed to start service Intent - Android Oreo

我目前使用的 startWakefulService 函数在 Oreo 中崩溃了。我意识到我要么必须切换到 startForegroundService() 并使用前台服务,要么切换到 JobIntentService 但根据下面的代码我不确定该怎么做。 (抱歉我是android新手)。任何正确方向的观点都将不胜感激。

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // Explicitly specify that GCMIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));

    setResultCode(Activity.RESULT_OK);
}
}

这是我在 Android 运行 8.x

时得到的当前错误

Fatal Exception: java.lang.RuntimeException Unable to start receiver com.heyjude.heyjudeapp.gcm.GcmBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.app.app cmp=com.app.app/.gcm.GCMIntentService (has extras) }: app is in background uid UidRecord{f602fba u0a114 RCVR idle procs:1 seq(0,0,0)}

基于documentation

As of Android O, background check restrictions make this class no longer generally useful. (It is generally not safe to start a service from the receipt of a broadcast, because you don't have any guarantees that your app is in the foreground at this point and thus allowed to do so.) Instead, developers should use android.app.job.JobScheduler to schedule a job, and this does not require that the app hold a wake lock while doing so (the system will take care of holding a wake lock for the job).

唯一的选择是启动 ForeGroundService 或使用 JobScheduler/JobIntentService

您可以启动前台服务如下:

@Override
public void onReceive(Context context, Intent intent) {

    // Explicitly specify that GCMIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    ContextCompat.startForegroundService(context,intent.setComponent(comp));
    ...
}

您还需要从服务 class 调用 startService() 并显示通知。您可以按照我对此 SO 的回答了解实施细节。

我遇到了同样的行为。

为什么会出现这个问题?

Due to the new Background Execution Limits of Android 8 and you should not start services background.

我是如何修复的

将您的 GCMIntetService 迁移到 JobIntentService 而不是 IntentService

请按照以下步骤操作: 1) 为您的服务添加BIND_JOB_SERVICE权限:

<service android:name=".service.GCMIntentService"
        android:exported="false"
        android:permission="android.permission.BIND_JOB_SERVICE"/>

2) 在您的 GCMIntentService 中,而不是扩展 IntentService,使用 android.support.v4.app.JobIntentService 并覆盖 onHandleWork 然后删除 onHandleIntent.

中的 override
public class GCMIntentService extends JobIntentService {

    // Service unique ID
    static final int SERVICE_JOB_ID = 50;

    // Enqueuing work in to this service.
    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, GCMIntentService.class, SERVICE_JOB_ID, work);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        onHandleIntent(intent);
    }

    private void onHandleIntent(Intent intent) {
        //Handling of notification goes here
    }
}

最后,在您的 GCMBroadcastReceiver 中,将您的 GCMIntentService 加入队列。

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        // startWakefulService(context, (intent.setComponent(comp)));

        //setResultCode(Activity.RESULT_OK);

        GCMIntentService.enqueueWork(context, (intent.setComponent(comp)));
    }
}

在我们将目标 sdk 更新到 27 后,这个实现对我有用,我希望它对你有用。