Android系统应用后台启动服务

本文讲述了在Android8.0及以后版本中,应用在后台试图启动服务可能导致IllegalStateException的问题,介绍了两种解决方案:使用前台服务避免限制,或在Manifest中通过设置android:sharedUserId为android.uid.system来获取系统权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.BOOT_COMPLETED flg=0x9000010 cmp=com.xxx.xxx/.service.MyIntentService (has extras) }: app is in background uid UidRecord{6ca99a3 u0a7 RCVR idle change:uncached procs:1 seq(0,0,0)}

这个错误是由于你的应用在后台时试图启动服务引起的。在 Android 中,自 Android 8.0(API 级别 26)起,后台限制(Background Execution Limits)被引入,这意味着在后台运行的应用有一些限制,比如不能随意启动服务。

方法1:前台服务
异常:app is in background uid UidRecord/stadid not then call Service.startForeground

方法2:获取system权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.xxx"
    android:sharedUserId="android.uid.system">

在manifest中加入android:sharedUserId=“android.uid.system”,然后重新做Android包。具体是不是一定要做Android包才能生效,我不清楚,只是提供一种解决思路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值