后台限制的发展历程
前台定义
针对后台Service
procState <= PROCESS_STATE_IMPORTANT_BACKGROUND=7
针对后台启动Activity
procState <= PROCESS_STATE_BOUND_TOP=3
针对后台启动FGS/后台启动FGS的while-in-use权限
procState <= PROCESS_STATE_BOUND_FOREGROUND_SERVICE=5
限制时打印的异常log
后台限制运行Service
03-22 01:49:59.872 3463 3521 W ActivityManager: Stopping service due to app idle: u0a153 -1m7s689ms com.qiyi.video/com.iqiyi.im.service.PPMessageService
后台限制启动Service
09-13 10:30:40.633 1581 2603 W ActivityManager: Background start not allowed: service Intent { cmp=com.android.deskclock/.addition.resource.ResourceLoadService (has extras) } to com.android.deskclock/.addition.resource.ResourceLoadService from pid=14625 uid=10216 pkg=com.android.deskclock startFg?=false
后台限制启动activity
12-15 08:36:11.818 1000 2014 7701 I ActivityTaskManager: START u0 {act=android.intent.action.VIEW dat=snssdk1128://webview/… flg=0x10000000 cmp=com.ss.android.ugc.aweme/.app.DeepLinkHandlerActivity}
from uid 10116 from pid 8466 callingPackage com.miui.personalassistant
12-15 08:36:11.819 1000 2014 7701 W ActivityTaskManager: Background activity start [callingPackage: com.miui.personalassistant; callingUid: 10116; appSwitchState: 2; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: BOUND_FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10116; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: BOUND_FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; allowBackgroundActivityStart: false; intent: Intent{ act=android.intent.action.VIEW dat=snssdk1128://webview/… flg=0x10000000 cmp=com.ss.android.ugc.aweme/.app.DeepLinkHandlerActivity }
后台启动的FGS限制使用while-in-use权限
07-15 18:08:27.172 1442 1458 W ActivityManager: Foreground service started from background can not have location/camera/microphone access: service com.xiaomi.discover/com.xiaomi.market.service.AppActiveStatService
后台限制启动FGS
12-17 01:14:55.156 1383 12145 W ActivityManager: Background started FGS: Disallowed [callingPackage: com.debug.loggerui; callingUid: 10102; uidState: SVC ; intent: Intent { cmp=com.debug.loggerui/.framework.DebugLoggerUIService }; code:DENIED; tempAllowListReason:; targetSdkVersion:31; callerTargetSdkVersion:31; startForegroundCount:0; bindFromPackage:null]
java.lang.RuntimeException: Unable to create service com.debug.loggerui.framework.DebugLoggerUIService: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.debug.loggerui/.framework.DebugLoggerUIService
常用豁免方法
针对后台广播限制解决
- 发送方发送显示广播
- 发送方添加FLAG_RECEIVER_INCLUDE_BACKGROUND flag
- 接收方使用动态注册方式
后台限制Service豁免
- 系统、用户或临时休眠许可名单中
后台限制启动activity豁免
- START_ACTIVITIES_FROM_BACKGROUND权限
- SYSTEM_ALERT_WINDOW 权限
- 应用中的某个服务被另一个可见应用绑定
- 应用中的某个服务被另一个应用以BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS flag绑定
后台限制启动FGS豁免
- 申请START_FOREGROUND_SERVICES_FROM_BACKGROUND权限
- 申请START_ACTIVITIES_FROM_BACKGROUND 权限
- 申请SYSTEM_ALERT_WINDOW权限
后台启动FGS限制while-in-use权限使用
- 申请START_ACTIVITIES_FROM_BACKGROUND 权限
通用豁免原因
可以豁免后台启动Activity、后台启动FGS、后台启动FGS的while-in-use权限
- START_ACTIVITIES_FROM_BACKGROUND 权限
- ROOT_UID/SYSTEM_UID/NFC_UID(SHELL_UID)等重要uid
- 应用程序有可见窗口(isUidForeground)
- 关于areBackgroundActivityStartsAllowed系列
可以豁免后台启动Activity、后台启动FGS
- 用户已向应用授予 SYSTEM_ALERT_WINDOW 权限
临时白名单
android 8.0+后台Service限制
idle白名单(mDeviceIdleAllowlist)或临时白名单(mDeviceIdleTempAllowlist或mPendingTempAllowlist)
android 12+从后台启动FGS限制
mDeviceIdleExceptIdleAllowlist 或 mFgsStartTempAllowList