我知道从Android API 29开始从后台启动活动的限制,但我受到一些应用程序的挑战,尽管有限制,但它能够呈现一个活动。该应用程序不使用SYSTEM_ALERT_WINDOW
权限绘制其他应用程序。它只是在只有前台服务运行的情况下呈现一个活动。
应用程序监听包添加(ACTION_PACKAGE_ADDED)和删除(ACTION_PACKAGE_REMOVED)广播,并启动一个活动样式为对话框
通常,当我注册接收器(无论是在manifest或运行时在前台服务),并希望启动一个活动,我得到下一个日志从ActivityTaskManager
:
2022-12-25 11:29:57.188 511-855 ActivityTaskManager pid-511 I START u0 {flg=0x10000000 cmp=com.jasperapps.testpackageremoveintent/.TargetActivity} from uid 10146
2022-12-25 11:29:57.190 511-855 ActivityTaskManager pid-511 W Background activity start [callingPackage: com.jasperapps.testpackageremoveintent; callingUid: 10146; appSwitchAllowed: false; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10146; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; allowBackgroundActivityStart: false; intent: Intent { flg=0x10000000 cmp=com.jasperapps.testpackageremoveintent/.TargetActivity }; callerApp: ProcessRecord{5224fb8 12583:com.jasperapps.testpackageremoveintent:remote/u0a146}; inVisibleTask: false]
2022-12-25 11:29:57.191 511-855 ActivityTaskManager pid-511 E Abort background activity starts from 10146
其中Abort background activity starts from 10146
表示我违反了背景限制,这是预期的。
相反,当我检查ActivityTaskManager
的logcat应用程序的解决方案时,我发现下面:
2022-12-25 11:34:59.196 511-4063 ActivityTaskManager pid-511 I START u0 {cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras)} from uid 10148
2022-12-25 11:34:59.198 511-4063 ActivityTaskManager pid-511 W Background activity start [callingPackage: com.bingo.cleaner; callingUid: 10148; appSwitchAllowed: false; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10148; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: PendingIntentRecord{921ede4 com.bingo.cleaner startActivity}; allowBackgroundActivityStart: false; intent: Intent { cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras) }; callerApp: ProcessRecord{c06f774 13034:com.bingo.cleaner/u0a148}; inVisibleTask: false]
2022-12-25 11:34:59.199 511-4063 ActivityTaskManager pid-511 W startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x800000 cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras) }
2022-12-25 11:34:59.200 511-4063 ActivityTaskManager pid-511 E Abort background activity starts from 10148
2022-12-25 11:34:59.209 511-857 ActivityTaskManager pid-511 I START u0 {flg=0x10000000 cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras)} from uid 10148
2022-12-25 11:34:59.210 511-857 ActivityTaskManager pid-511 W Background activity start [callingPackage: com.bingo.cleaner; callingUid: 10148; appSwitchAllowed: false; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10148; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; allowBackgroundActivityStart: false; intent: Intent { flg=0x10000000 cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras) }; callerApp: ProcessRecord{c06f774 13034:com.bingo.cleaner/u0a148}; inVisibleTask: false]
2022-12-25 11:34:59.210 511-857 ActivityTaskManager pid-511 E Abort background activity starts from 10148
2022-12-25 11:34:59.285 511-857 ActivityTaskManager pid-511 I START u0 {cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras)} from uid 10148
2022-12-25 11:34:59.287 511-857 ActivityTaskManager pid-511 W Background activity start [callingPackage: com.bingo.cleaner; callingUid: 10148; appSwitchAllowed: false; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10148; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: PendingIntentRecord{921ede4 com.bingo.cleaner startActivity (allowlist: d43f3f6:+30s0ms/0/NOTIFICATION_SERVICE/NotificationManagerService)}; allowBackgroundActivityStart: false; intent: Intent { cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras) }; callerApp: ProcessRecord{c06f774 13034:com.bingo.cleaner/u0a148}; inVisibleTask: false]
2022-12-25 11:34:59.287 511-857 ActivityTaskManager pid-511 W startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x800000 cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras) }
2022-12-25 11:34:59.288 511-857 ActivityTaskManager pid-511 E Abort background activity starts from 10148
2022-12-25 11:34:59.288 511-857 ActivityTaskManager pid-511 I START u0 {flg=0x10000000 cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras)} from uid 10148
2022-12-25 11:34:59.288 511-857 ActivityTaskManager pid-511 W Background activity start [callingPackage: com.bingo.cleaner; callingUid: 10148; appSwitchAllowed: false; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10148; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; allowBackgroundActivityStart: false; intent: Intent { flg=0x10000000 cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras) }; callerApp: ProcessRecord{c06f774 13034:com.bingo.cleaner/u0a148}; inVisibleTask: false]
2022-12-25 11:34:59.289 511-857 ActivityTaskManager pid-511 E Abort background activity starts from 10148
2022-12-25 11:34:59.522 511-543 ActivityTaskManager pid-511 I START u0 {cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras)} from uid 10148
2022-12-25 11:34:59.522 511-543 ActivityTaskManager pid-511 W startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x800000 cmp=com.bingo.cleaner/.modules.scene.fork.ForkActivityC (has extras) }
2022-12-25 11:34:59.527 511-543 ActivityTaskManager pid-511 W Can't find TaskDisplayArea to determine support for multi window. Task id=27 attached=false
2022-12-25 11:34:59.606 511-852 ActivityTaskManager pid-511 W Tried to set launchTime (0) < mLastActivityLaunchTime (556847)
2022-12-25 11:34:59.914 511-546 ActivityTaskManager pid-511 I Displayed com.bingo.cleaner/.modules.scene.fork.ForkActivityC: +325ms
注意,我有意在logcat中添加了空行,以使其更清晰。因此,应用程序尝试启动一个活动三次,并被系统拒绝。然而在第四次的时候,它却成功了!
请看看我的屏幕录制视频:https://yura-misc.s3.eu-central-1.amazonaws.com/activity_from_bakcground.mov
注:我所尝试的(从运行时注册的广播接收器的onReceive):
- 在循环中启动活动最多10次(以复制示例应用程序正在做的事情)
- 显示一个通知随着开始的活动(我认为通知可以增加我的应用程序的优先级)
- 启动另一个前台服务并从它启动活动(我认为它也可以增加我的应用程序的优先级)
- 发送Pending Intent而不是普通Intent来启动活动
我想这一定是某种黑客攻击,这是潜在的android系统漏洞。你能告诉我应该是什么或者我应该朝哪个方向看吗?
我也面临着类似的问题。我有一个数据库,其中电话号码节省时间,我的应用程序需要调用这些号码,即使我的应用程序在后台。但当我在android 12模拟器上试用时,效果很好。在后台工作,但电话一开始就中断了。当我在android 10上试用时。它不能在后台工作,但在前景工作得很好。
我在那个应用程序上也有一个通知,所以这个过程还没有完成。我不知道,现在该怎么办!
你可以在这里看到。您必须获得用户的SYSTEM_ALERT_WINDOW权限。此外,这里给出了多个条件。检查,如果你的解决了,请在这里写。我的问题还没解决。https://developer.android.com/guide/components/activities/background-starts