Java 不允许启动服务 Intent - Android Oreo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51353493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:05:46  来源:igfitidea点击:

Not allowed to start service Intent - Android Oreo

javaandroidandroid-8.0-oreo

提问by Marcus Smith

I am currently using the startWakefulService function which is crashing in Oreo. I realise I either have to switch to startForegroundService() and use a foreground service, or switch to JobIntentService but based on my code below I am not sure what to do. (Sorry I am an android novice). Any points in the right direction will be greatly appreciated.

我目前正在使用在 Oreo 中崩溃的 startWakefulService 函数。我意识到我要么必须切换到 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);
}
}

This is the current error I get when run on Android 8.x

这是我在 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)}

致命异常:java.lang.RuntimeException 无法启动接收器 com.heyjude.heyjudeapp.gcm.GcmBroadcastReceiver:java.lang.IllegalStateException:不允许启动服务 Intent { act=com.google.android.c2dm.intent.RECEIVE flg= 0x1000010 pkg=com.app.app cmp=com.app.app/.gcm.GCMIntentService(有附加功能)}:应用在后台 uid UidRecord{f602fba u0a114 RCVR idle procs:1 seq(0,0,0)}

采纳答案by Abdulmoiz Esmail

I experience the same behaviour.

我经历了同样的行为。

Why this issue happens?

为什么会出现这个问题?

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

由于Android 8的新后台执行限制,您不应启动服务后台。

How I fixed it

我是如何修复它的

Migrate your GCMIntetServiceto JobIntentServiceinstead of IntentService.

迁移GCMIntetServiceJobIntentService而不是IntentService.

Please follow this steps: 1) Add the BIND_JOB_SERVICE permission to your service:

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

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

2) In your GCMIntentService, instead extending the IntentService, use android.support.v4.app.JobIntentServiceand override onHandleWork then remove the overridein you onHandleIntent.

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

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

Then finally, in your GCMBroadcastReceiver, enqueue your GCMIntentService.

然后最后,在您的 中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)));
    }
}

This implementation work for me after we update our target sdk to 27 and I hope it works for you.

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

回答by Sagar

Based on the 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).

从 Android O 开始,背景检查限制使此类不再普遍有用。(从接收到广播开始服务通常是不安全的,因为此时您无法保证您的应用程序在前台,因此允许这样做。)相反,开发人员应该使用 android。 app.job.JobScheduler 来安排作业,这不需要应用程序在执行此操作时持有唤醒锁(系统将负责为作业持有唤醒锁)。

Only alternative is to start a ForeGroundService or use JobScheduler/JobIntentService

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

You can start foreground service as follows:

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

@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));
    ...
}

You also need to call startService()from your Service class and display notification. You can follow my answer on this SOfor implementation details.

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