版权声明:本文为博主原创文章,转载请务必注明作者与原文链接。 https://blog.csdn.net/jingerppp/article/details/82258858
先来看下log:
11-05 04:47:32.468 911 911 E AndroidRuntime: FATAL EXCEPTION: main
11-05 04:47:32.468 911 911 E AndroidRuntime: Process: com.shift.phonemanager, PID: 911
11-05 04:47:32.468 911 911 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.shift.phonemanager.apps.accesslog.PermissionAccessStartReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.shift.phonemanager/.apps.accesslog.service.PermissionAccessLogService }: app is in background uid UidRecord{a423648 u0a80 RCVR idle change:uncached procs:1 seq(0,0,0)}
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3197)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.-wrap17(Unknown Source:0)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1675)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6518)
11-05 04:47:32.468 911 911 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-05 04:47:32.468 911 911 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
11-05 04:47:32.468 911 911 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
11-05 04:47:32.468 911 911 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.shift.phonemanager/.apps.accesslog.service.PermissionAccessLogService }: app is in background uid UidRecord{a423648 u0a80 RCVR idle change:uncached procs:1 seq(0,0,0)}
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1477)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:650)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:650)
11-05 04:47:32.468 911 911 E AndroidRuntime: at com.shift.phonemanager.apps.accesslog.PermissionAccessStartReceiver.startAccessLogService(PermissionAccessStartReceiver.java:22)
11-05 04:47:32.468 911 911 E AndroidRuntime: at com.shift.phonemanager.apps.accesslog.PermissionAccessStartReceiver.onReceive(PermissionAccessStartReceiver.java:18)
11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3190)
11-05 04:47:32.468 911 911 E AndroidRuntime: ... 8 more
该Exception 是从ContextImpl.java 中抛出来的,详细可以看 Android service 启动篇之 startService
代码如下:
private ComponentName startServiceCommon(Intent service, boolean requireForeground,
UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess(this);
ComponentName cn = ActivityManager.getService().startService(
mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
getContentResolver()), requireForeground,
getOpPackageName(), user.getIdentifier());
if (cn != null) {
if (cn.getPackageName().equals("!")) {
throw new SecurityException(
"Not allowed to start service " + service
+ " without permission " + cn.getClassName());
} else if (cn.getPackageName().equals("!!")) {
throw new SecurityException(
"Unable to start service " + service
+ ": " + cn.getClassName());
} else if (cn.getPackageName().equals("?")) {
throw new IllegalStateException(
"Not allowed to start service " + service + ": " + cn.getClassName());
}
}
return cn;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
当返回的ComponentName 的package name 为 "?" 的时候就会抛出这个异常。
通过Android service 启动篇之 startService 的 3.3.1 中我们可以知道,必须要满足一定的条件,应用才能启动后台服务。
结论:
顺利启动service,需要满足下面的条件:
- app 为persistent
- 或 service 的uid 在background 的白名单中
- 或 service 的uid 在device id 的白名单中
- 对于 service 的应用SDK 版本小于O(26),而且AppOpsManager 中是allowed 状态
- 对于SDK 大于等于O(26)的service,不满足上面条件只能选择前台服务,通过startForegroundService 启动
详细 service 的机制可以看 Android 中service 详解