Not allowed to start service Intent { xxxxx }: app is in background uid null
android A应用启动B应用里的service?
根据AMS的代码:
如果B应用没有运行(即没有B进程在运行),startservice()会启动一个新的B进程,但是为啥实际测试时的现象是:如果B应用没有运行,服务启动不了,会报错:
Not allowed to start service Intent { xxxxx }: app is in background uid null
网上有同学说的是,Android 8.0以前是可以的,不会报错,但是Android 8.0 有一项复杂功能:系统不允许后台应用创建后台服务,只能通过Context.startForegroundService()启动前台服务。
解决:
启动service的app和在目标service的app 的AndroidManifest.xml里都添加:
android:sharedUserId="android.uid.system"
参考:
android8.0以上版本的前台服务
Android在一个app中启动其他app中的service或者Activity
Android service 启动篇之 startForegroundService