在应用程序启动时,应用程序启动应该执行一些网络任务的服务。 在目标API级别26后,我的应用程序无法在Android 8.0后台启动服务。

导致原因:java.lang.IllegalStateException:不允许启动 服务意图{ cmp = my.app.tt / com.my.service }: app是在后台uid UidRecord{90372b1 u0a136 CEM空闲procs:1 seq (0, 0, 0)}

我的理解是: 后台执行限制

startService()方法现在抛出一个IllegalStateException 针对Android 8.0的应用程序尝试使用这种方法 不允许创建后台服务。

“在不被允许的情况下”——这实际上是什么意思?以及如何修复它。我不想把我的服务设置为前台


当前回答

如果任何意图之前工作正常时,应用程序是在后台,这将不再是Android 8及以上的情况下。只引用意图,当app在后台时,它必须做一些处理。

必须遵循以下步骤:

Above mentioned intent should be using JobIntentService instead of IntentService. The class which extends JobIntentService should implement the - onHandleWork(@NonNull Intent intent) method and should have below the method, which will invoke the onHandleWork method: public static void enqueueWork(Context context, Intent work) { enqueueWork(context, xyz.class, 123, work); } Call enqueueWork(Context, intent) from the class where your intent is defined. Sample code: Public class A { ... ... Intent intent = new Intent(Context, B.class); //startService(intent); B.enqueueWork(Context, intent); }

下面的类以前扩展了Service类

Public Class B extends JobIntentService{
...

    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, B.class, JobId, work);
    }

    protected void onHandleWork(@NonNull Intent intent) {
        ...
        ...
    }
}

com.android。支持:支持compat需要JobIntentService -我使用26.1.0 V。 最重要的是确保Firebase库的版本至少是10.2.1,我在10.2.0有问题——如果你有的话! 你的manifest应该具有Service类的以下权限: 服务android: name = "。B” android:出口= " false " android:许可= " android.permission.BIND_JOB_SERVICE "

希望这能有所帮助。

其他回答

是的,那是因为在API 26上你不能在后台启动服务了。所以你可以在API 26之上启动ForegroundService。

你必须使用

ContextCompat.startForegroundService(...)

并在处理泄漏时发布通知。

如果你有集成的firebase消息推送通知,

由于后台执行限制,为android O (android 8.0)添加新的/更新firebase消息依赖项。

compile 'com.google.firebase:firebase-messaging:11.4.0'

如果需要,升级谷歌播放服务和谷歌存储库。

更新:

 compile 'com.google.firebase:firebase-messaging:11.4.2'

由于对这个答案的争议投票(+4/-4),请先看看其他答案,并仅作为最后的手段。我只在一个以root身份运行的网络应用程序中使用过一次,我同意一般的意见,即在正常情况下不应该使用这种解决方案。

原答案如下:

其他答案都是正确的,但我想指出的是,另一种解决这个问题的方法是要求用户禁用应用程序的电池优化(这通常不是一个好主意,除非你的应用程序与系统相关)。看看这个答案如何请求选择退出电池优化而不让你的应用程序在谷歌播放被禁止。

你也应该检查电池优化是否在你的接收器关闭,以防止崩溃通过:

if (Build.VERSION.SDK_INT < 26 || getSystemService<PowerManager>()
        ?.isIgnoringBatteryOptimizations(packageName) != false) {
    startService(Intent(context, MyService::class.java))
} // else calling startService will result in crash

从firebase发布说明中,他们指出对Android O的支持首次发布于10.2.1(尽管我建议使用最新版本)。

请为android O添加新的firebase消息依赖项

compile 'com.google.firebase:firebase-messaging:11.6.2'

如果需要,升级谷歌播放服务和谷歌存储库。

我看到很多回复都建议只使用前台服务。为了使用ForegroundService,必须有一个与之关联的通知。用户将看到此通知。根据不同的情况,他们可能会对你的应用感到厌烦并卸载它。

最简单的解决方案是使用名为WorkManager的新的体系结构组件。您可以在这里查看文档:https://developer.android.com/topic/libraries/architecture/workmanager/

您只需定义扩展worker的worker类。

public class CompressWorker extends Worker {

    public CompressWorker(
        @NonNull Context context,
        @NonNull WorkerParameters params) {
        super(context, params);
    }

    @Override
    public Worker.Result doWork() {

        // Do the work here--in this case, compress the stored images.
        // In this example no parameters are passed; the task is
        // assumed to be "compress the whole library."
        myCompress();

        // Indicate success or failure with your return value:
        return Result.SUCCESS;

        // (Returning RETRY tells WorkManager to try this task again
        // later; FAILURE says not to try again.)
    }
}

然后您可以安排您想要运行它的时间。

    OneTimeWorkRequest compressionWork = 
        new OneTimeWorkRequest.Builder(CompressWorker.class)
            .build();
    WorkManager.getInstance().enqueue(compressionWork);

简单!有很多方法可以配置worker。它支持重复作业,如果需要的话,你甚至可以做一些复杂的事情,比如链接。希望这能有所帮助。