【问题标题】:Android 11 - java.lang.IllegalStateException Not allowed to start service Intent : app is in background uid UidRecord{}Android 11 - java.lang.IllegalStateException 不允许启动服务意图:应用处于后台 uid UidRecord{}
【发布时间】:2020-10-26 09:17:28
【问题描述】:

在适用于 Pixel 设备的 Android 11 上报告了以下崩溃。

致命异常:java.lang.RuntimeException 无法启动活动 ComponentInfo{}:java.lang.IllegalStateException:不允许启动服务 Intent {}:应用处于后台 uid UidRecord{}

这是我上面异常指向的代码。

   val stickyService = Intent(this, StickyService::class.java)
   startService(stickyService)

我的 StickyService 类扩展了覆盖以下方法的服务类。

    public class StickyService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    **Update value in preferences here**
}}

这仅在 Android 11 设备上发生。 Google 在 Android 11 中的服务实现是否有重大变化?

【问题讨论】:

    标签: android service


    【解决方案1】:

    尝试使用前台服务,因为后台服务是不允许的> android 8 当应用被杀死

    【讨论】:

    • Android 11对Service的限制和之前的版本一样,要在应用程序在后台启动服务,服务应该作为前台服务启动并且应用程序应该显示一个通知对于前台服务,developer.android.com/guide/components/foreground-services
    • 我们如何使用前台服务?需要改变什么?
    【解决方案2】:

    Android 11 中的应用可见性发生了变化。

    如果applicationA使用的是applicationB的服​​务,需要在applicationA的AndroidManifest.xml中声明:

    <manifest package="com.exampleA.applicationA">
        <queries>
            <package android:name="com.exampleB.applicationB" />
        </queries>
        ...
    </manifest>
    

    所以 android 将使 applicationB 对 applicationA 可见。

    来源:https://developer.android.com/training/package-visibility/declaring

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2019-11-21
      • 2011-11-27
      • 2019-01-31
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多