ANR是Application Not Response的简写就是应用没有响应。Android应用主线程卡住的时候系统会提示用户是否需要结束掉此App,这是Android系统优化用户体验的一种做法,类似的Windows系统也有类似“程序没有响应”的提示。就是主线程无法及时响应用户输入
1 2 3
E/ActivityManager( 373): ANR in com.android.settings (com.android.settings/.Settings) E/ActivityManager( 373): PID: 803 E/ActivityManager( 373): Reason: Input dispatching timed out (Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.)
原因
主线程被阻塞
主线程有耗时操作,比如IO
主线程异常操作比如Thread.sleep,Thread.wait
(Activity)应用在5秒内没有响应用户输入(例如键盘输入, 触摸屏幕等)
BroadcastReceiver10秒钟没有响应
获取不到CPU时间片(CPU太满了)
CPU占有率100%
1 2 3 4 5 6 7 8 9 10 11 12
E/ActivityManager( 373): ANR in com.android.settings (com.android.settings/.Settings) E/ActivityManager( 373): PID: 803 E/ActivityManager( 373): Reason: Input dispatching timed out (Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.) Load: 1.43 / 2.3 / 1.84 //表示ANR发生之前的一段时间内的CPU使用率,并不是某一时刻的值 CPU usage from 810ms to -13732ms ago: 63% 803/com.android.settings: 17% user + 45% kernel / faults: 3557 minor 225 major 55% 373/system_server: 22% user + 33% kernel / faults: 3423 minor ... 1% 569/android.process.acore: 0% user + 1% kernel / faults: 1 minor 1% 777/Binder_4: 0% user + 1% kernel 100% TOTAL: 39% user + 60% kernel