当前位置:   article > 正文

java.lang.IllegalStateException: Not allowed to start service Intent

java.lang.illegalstateexception: not allowed to start service intent { cmp=c

先来看下log:

  1. 11-05 04:47:32.468 911 911 E AndroidRuntime: FATAL EXCEPTION: main
  2. 11-05 04:47:32.468 911 911 E AndroidRuntime: Process: com.shift.phonemanager, PID: 911
  3. 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)}
  4. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3197)
  5. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.-wrap17(Unknown Source:0)
  6. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1675)
  7. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
  8. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
  9. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6518)
  10. 11-05 04:47:32.468 911 911 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
  11. 11-05 04:47:32.468 911 911 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  12. 11-05 04:47:32.468 911 911 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  13. 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)}
  14. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
  15. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1477)
  16. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:650)
  17. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:650)
  18. 11-05 04:47:32.468 911 911 E AndroidRuntime: at com.shift.phonemanager.apps.accesslog.PermissionAccessStartReceiver.startAccessLogService(PermissionAccessStartReceiver.java:22)
  19. 11-05 04:47:32.468 911 911 E AndroidRuntime: at com.shift.phonemanager.apps.accesslog.PermissionAccessStartReceiver.onReceive(PermissionAccessStartReceiver.java:18)
  20. 11-05 04:47:32.468 911 911 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3190)
  21. 11-05 04:47:32.468 911 911 E AndroidRuntime: ... 8 more

该Exception 是从ContextImpl.java 中抛出来的,详细可以看 Android service 启动篇之 startService

 

代码如下:

  1. private ComponentName startServiceCommon(Intent service, boolean requireForeground,
  2. UserHandle user) {
  3. try {
  4. validateServiceIntent(service);
  5. service.prepareToLeaveProcess(this);
  6. ComponentName cn = ActivityManager.getService().startService(
  7. mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
  8. getContentResolver()), requireForeground,
  9. getOpPackageName(), user.getIdentifier());
  10. if (cn != null) {
  11. if (cn.getPackageName().equals("!")) {
  12. throw new SecurityException(
  13. "Not allowed to start service " + service
  14. + " without permission " + cn.getClassName());
  15. } else if (cn.getPackageName().equals("!!")) {
  16. throw new SecurityException(
  17. "Unable to start service " + service
  18. + ": " + cn.getClassName());
  19. } else if (cn.getPackageName().equals("?")) {
  20. throw new IllegalStateException(
  21. "Not allowed to start service " + service + ": " + cn.getClassName());
  22. }
  23. }
  24. return cn;
  25. } catch (RemoteException e) {
  26. throw e.rethrowFromSystemServer();
  27. }
  28. }

当返回的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 详解

 

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号