gpt4 book ai didi

android - 带有 boot_completed 的运行时异常 Android O

转载 作者:IT老高 更新时间:2023-10-28 21:58:18 26 4
gpt4 key购买 nike

我正在尝试在我的 BOOT_COMPLETED 接收器中启动 IntentService,但在 Android O (API 26) 中我得到:

java.lang.RuntimeException: 
java.lang.IllegalStateException:
Not allowed to start service Intent { act=intent.action.update cmp=packageName.services.OwnService }:
app is in background

(消息在一行中,但这样更容易阅读)

我怎样才能以正确的方式做到这一点?

最佳答案

以下是我在 a blog post 中概述的一些选项:

解决方法 #1:startForegroundService()

您的 BroadcastReceiver 接收 ACTION_BOOT_COMPLETED 广播在 Android 上可以调用 startForegroundService() 而不是 startService()8.0+:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

public class OnBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context, TestIntentService.class);

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
context.startForegroundService(i);
}
else {
context.startService(i);
}
}
}

请注意,这在一定程度上是有效的,即使您的服务实际上并没有曾经调用 startForeground()。你有一个时间来解决调用 startForeground(),“与执行此操作的 ANR 间隔相当”。如果你的工作时间超过一毫秒但不到几秒,您可以跳过 NotificationstartForeground() 调用。然而,你会在 LogCat 中得到一个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.commonsware.myapplication, PID: 5991
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1775)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

当然,如果您不介意简短的 Notification,欢迎您像 Android 期望的那样使用 startForeground(),在这种情况下,你可以后台正常工作,尽管用户通知中显示了一个条目阴影。

解决方法 #2:goAsync()

BroadcastReceiver 从 API 级别 11 开始提供 goAsync()。这允许您的接收器完成主应用程序线程的工作,因此您可以摆脱IntentService 并将您的代码移动到 BroadcastReceiver。你仍然只有 ANR可以使用的超时时间,但您不会占用您的主应用程序线。这比第一种解决方法要好,因为它具有相同的时间限制,但避免了令人讨厌的错误。但是,它确实需要一些数量返工。

解决方法 #3:JobScheduler

如果您的工作将花费超过几秒钟的时间并且您希望避免Notification,你可以修改你的代码来实现一个 JobService 和使用 JobScheduler。这有一个额外的好处是只给你控制何时满足其他标准(例如,有可用的互联网联系)。但是,这不仅需要重写,而且 JobScheduler仅适用于 Android 5.0+,因此如果您的 minSdkVersion 小于 21,您将需要在旧设备上使用其他解决方案。

更新:Eugen Pechanec pointed out JobIntentService ,这是一个有趣的 JobService/IntentService 混搭。

关于android - 带有 boot_completed 的运行时异常 Android O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44502229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com