不允许启动服务Intent - Android Oreo

问题描述 投票:4回答:2

我目前正在使用在奥利奥崩溃的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);
}
}

这是我在Android 8.x上运行时遇到的当前错误

致命异常: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(has extras)}:app在后台uid UidRecord {f602fba u0a114 RCVR idle procs:1 seq(0,0,0)}

java android android-8.0-oreo
2个回答
15
投票

我经历了同样的行为。

为什么会出现此问题?

由于Android 8的新Background Execution Limits,你不应该启动服务背景。

我是如何修理它的

将你的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然后删除你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
    }
}

最后,在你的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后,此实现对我有用,我希望它对您有用。


1
投票

基于documentation

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

唯一的选择是启动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));
    ...
}

您还需要从Service类调用startService()并显示通知。您可以按照我在SO上的答案获取实施细节。

© www.soinside.com 2019 - 2024. All rights reserved.