【AMS】Android 8.0+ 绕开启动后台Service限制

server/2024/10/9 15:25:43/

一、背景

应客户要求,需要在开机时,拉起应用A。但因为开机时,同时被拉起的应用过多,导致Launcher在开机那一刻较为卡顿。为解决这一问题,采取了延迟拉起的做法。在开机后,延迟一定时间,由系统服务,拉起应用A。

于是乎,就出现这么个报错:

Not allowed to start service Intent { cmp=com.xxx.xxx/.XXXXService }: app is in background

二、解决方案

方案一:系统签名

解决这个报错,有个较为简单的做法,是将应用A带上系统签名,但考虑到应用A后续有他们自己的OTA计划,我们不可能把系统的签名文件给他们(涉及安全)。他们也不可能更新应用时,再针对我们的产品,交给我们签名,再发布特定渠道的应用(流程繁甭)。因此,此方案不可取。

方案二:透明Activity

拉起应用A的透明Activity,再由应用A的透明Activity拉起服务并Finish掉Activity。 这个方案,听起来可以,但实际操作上会存在一个问题,即在被拉起的一瞬间,用户的操作会被打断,出错短暂的卡顿现象以及焦点变化。因为是延时拉起,不知道此时用户在做什么操作,导致的后续问题也是未知的,因此,此方案也不可取。

方案三:绕过限制

基于前2种方案, 最终决定,对限制的原因,进行分析。

  • 分析报错堆栈
2024-05-20 20:27:48.742   549-1975  Activi...nager pid-549              W  Background start not allowed: service Intent { cmp=com.xxx/.XXXService } to com.xxxx/.XXXService from pid=1061 uid=1000 pkg=com.xxxx.xxx startFg?=false
2024-05-20 20:27:48.743  1061-1061  XXXXHelper  com...e.xxx  D  start XXXService failed1111 = Not allowed to start service Intent { cmp=com.xxxx/.XXXService }: app is in background uid null
2024-05-20 20:27:48.743  1061-1061  System.err     com...e.xxx  W  android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { cmp=com.xxxx/.XXXService }: app is in background uid null
2024-05-20 20:27:48.743  1061-1061                 com...e.xxx  W          at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1908)
2024-05-20 20:27:48.743  1061-1061                 com...e.xxx  W          at android.app.ContextImpl.startService(ContextImpl.java:1864)
2024-05-20 20:27:48.743  1061-1061                 com...e.xxx  W          at com.xxxx.xx.XXXXHelper.startXXXXService(XXXXHelper.java:36)
2024-05-20 20:27:48.744  1061-1061                 com...e.xxx  W          at com.xxxx.xx.XXXXHelper.delayBootApps(XXXXHelper.java:25)
2024-05-20 20:27:48.744  1061-1061                 com...e.xxx  W          at com.xxxx.XXAppService.delayBootApps(XXAppService.java:1161)
2024-05-20 20:27:48.744  1061-1061                 com...e.xxx  W          at com.xxxx.XXAppService.-$$Nest$mdelayBootApps(Unknown Source:0)
2024-05-

http://www.ppmy.cn/server/44685.html

相关文章

mock.js和apifox模拟接口的能力

mock.js 和 Apifox 都是前端开发中常用的工具,用于模拟后端接口和数据。下面是它们的主要特点和模拟接口的能力的比较: mock.js mock.js 是一个用于生成随机数据的 JavaScript 库。它允许你定义数据模板,并生成模拟数据。mock.js 主要用于前…

Springboot集成GRPC

Springboot集成GRPC 一、springboot版本二、GRPC的pom依赖2.1 服务端2.2 客户端3.构建依赖 三、配置文件服务端客户端 四、 demo4.1 编写proto文件4.2 生成文件4.3 服务端重写方法4.4 客户端调用该方法 五、测试 一、springboot版本 <groupId>org.springframework.boot&l…

屎山代码SSM转换Springboot

SSM项目转Springboot项目 最近很多人可能是在网上买的那种屎山代码&#xff0c;数据库都是拼音的那种 比如项目如下所示&#xff1a; 这种屎山代码我改过太多了&#xff0c;很多人可能无从下手&#xff0c;因为代码结构太混乱了&#xff0c;但是我改过太多这种代码&#xff0…

5月28号总结

刷题记录 1.A. Phone Desktop 输入&#xff1a; 11 1 1 7 2 12 4 0 3 1 0 8 1 0 0 2 0 15 0 8 2 0 9 输出&#xff1a; 1 1 2 2 1 1 0 1 1 2 5 题意&#xff1a;题目给我们1x1和2x2的图标个数&#xff0c;让我们求最少需要多少个5x3的屏幕。 思路&#xff1a;当只看2x2的图…

layui自定义admin页面简单实现-练手

代码 免积分下载 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"utf-8"/><title>管理界面</title><meta name"renderer" content"webkit"/><meta http-equiv"X-UA-C…

用python做的一个登录界面——浔川python社

以下代码是一个基于Tkinter的简单的登录注册界面。 代码的功能如下&#xff1a; 导入tkinter模块&#xff0c;并重命名为tk。 创建一个窗口对象window&#xff0c;并设置标题和大小。 创建一个随机的四位数作为验证码&#xff0c;并将其显示在窗口中。 创建用户名、密码和验…

工作五年后的程序员,一般怎样了?

本科一般是22岁毕业&#xff0c;5年开发经验一般是指27岁。这个阶段&#xff0c;不少程序员可能通过多次跳槽&#xff0c;薪资有一定涨幅&#xff0c;但真有可能还在小公司甚至外包公司做增删改查的业务&#xff0c;然后坐等30岁的到来。 就以Java为例&#xff0c;一些发展情况…

Codeforces Global Round 17 C. Keshi Is Throwing a Party 二分查找

Keshi Is Throwing a Party 题目描述 Keshi is throwing a party and he wants everybody in the party to be happy. He has n n n friends. His i i i-th friend has i i i dollars. If you invite the i i i-th friend to the party, he will be happy only if at m…