赞
踩
上一次对于broadcast 的机制进行了详细的解析,见博文 android 中的 Broadcast 机制详解 ,本篇对于四大组件中的另一个成员—— service 进行解读。
在开发中我们会碰到下面几种情况:
1、java.lang.IllegalStateException: Not allowed to start service Intent
- 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
2、Bringing down service while still waiting for start foreground
11-05 01:47:21.268 1552 1570 W ActivityManager: Bringing down service while still waiting for start foreground: ServiceRecord{c1c6d87 u0 com.shift.phonemanager/.apps.accesslog.service.PermissionAccessLogService}
3、Context.startForegroundService() did not then call Service.startForeground()
- 11-05 01:47:21.506 1552 1570 E ActivityManager: ANR in .accesslog
- 11-05 01:47:21.506 1552 1570 E ActivityManager: PID: 2509
- 11-05 01:47:21.506 1552 1570 E ActivityManager: Reason: Context.startForegroundService() did not then call Service.startForeground()
- 11-05 01:47:21.506 1552 1570 E ActivityManager: Load: 0.0 / 0.0 / 0.0
- 11-05 01:47:21.506 1552 1570 E ActivityManager: CPU usage from 7022ms to 0ms ago (2007-11-05 01:47:14.249 to 2007-11-05 01:47:21.271):
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 49% 1552/system_server: 24% user + 24% kernel / faults: 2576 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 19% 2306/android.process.media: 12% user + 6.6% kernel / faults: 5358 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 14% 1592/adbd: 3% user + 11% kernel / faults: 4277 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 13% 2207/com.android.launcher3: 11% user + 2.1% kernel / faults: 6393 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 9.1% 254/logd: 2.5% user + 6.5% kernel / faults: 30 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 7.1% 1472/zygote: 0.4% user + 6.7% kernel / faults: 3227 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 5.1% 1779/.dataservices: 3.8% user + 1.2% kernel / faults: 2197 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 5% 1805/com.android.phone: 2.2% user + 2.7% kernel / faults: 852 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 4.1% 1709/com.android.systemui: 3.7% user + 0.4% kernel / faults: 76 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 3.7% 518/logcat: 1.8% user + 1.8% kernel / faults: 5 minor
- 11-05 01:47:21.506 1552 1570 E ActivityManager: 3.4% 1470/surfaceflinger: 2.8% user + 0.5% kernel / faults: 160 minor
-
- ...
- ...
4、java.lang.SecurityException: Unable to start service
5、java.lang.SecurityException: Not allowed to start service
带着这么多的问题,有必要对于Android 中的service 进行剖析。将 AMS 中service 机制解析分以下几部分:
Android service 基础篇:
Android service 启动篇:
Android service 启动篇之 startService
Android service 启动篇之 bindService
Android service 启动篇之 startForegroundService
Android service 停止篇:
Android service 异常篇:
java.lang.IllegalStateException: Not allowed to start service Intent
Context.startForegroundService() did not then call Service.startForeground()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。