不允许启动服务意图 - Android Oreo

4

我目前正在使用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上运行时出现的当前错误。
致命异常: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(有extras)}:应用程序在后台uid UidRecord {f602fba u0a114 RCVR idle procs:1 seq(0,0,0)}。
2个回答

18

我经历了相同的行为。

为什么会出现这个问题?

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

我是如何解决它的

将您的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之后,这个实现对我有效,我希望它也能对你有效。


WakefulBroadcastReceiver已经被弃用了? - Josh
@DylanBurton,由于后台检查限制,在支持Android Oreo时它已不再有用。 - Abdulmoiz Esmail
如何使用闹钟管理器来实现这个功能(尝试安排一个闹钟)? - TheLearner

2
根据文档

从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答案以获取实现细节。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接