1、ANR是什么?
ANR英文全称Application Not Responding,通俗来说就是程序未响应!如果一个应用无法响应用户的输入,系统就会弹出一个ANR的对话框,如下图所示,用户可以自行选择继续等待还是停止当前程序。
2、出现场景
1、主线程被IO操作阻塞,
2、主线程做耗时操作,
3、主线程错误的操作,如Thread.wait或者Thread.sleep等
Android系统出现下面两种情况,就会弹出ANR对话框
1、应用在5秒内未响应用户输入事件
2、BroadcastReceiver未在10秒内完成相关处理
如何避免
耗时的操作全部交给子工作线程来处理,UI线程只做ui方面的操作。
1、使用AsyncTask处理耗时IO操作。
2、使用Thread或者HandlerThread时,调用Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)设置优先级,否则仍然会降低程序响应,因为默认Thread的优先级和主线程相同。
3、使用Handler处理工作线程结果,而不是使用Thread.wait()或者Thread.sleep()来阻塞主线程。
4、Activity的onCreate和onResume回调中尽量避免耗时的代码
5、BroadcastReceiver中onReceive代码也要尽量减少耗时,建议使用IntentService处理。
如何定位
如果开发机器上出现问题,我们可以通过查看/data/anr/traces.txt即可,最新的ANR信息在最开始部分。我们从stacktrace中即可找到出问题的具体行数。本例中问题出现在MainActivity.java 27行,因为这里调用了Thread.sleep方法。
root@htc_m8tl:/ # cat /data/anr/traces.txt | more
----- pid 30307 at 2015-05-30 14:51:14 -----
Cmd line: com.example.androidyue.bitmapdemo
JNI: CheckJNI is off; workarounds are off; pins=0; globals=272
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)
"main" prio=5 tid=1 TIMED_WAIT
| group="main" sCount=1 dsCount=0 obj=0x416eaf18 self=0x416d8650
| sysTid=30307 nice=0 sched=0/0 cgrp=apps handle=1074565528
| state=S schedstat=( 0 0 0 ) utm=5 stm=4 core=3
at java.lang.VMThread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:1044)
at java.lang.Thread.sleep(Thread.java:1026)
at com.example.androidyue.bitmapdemo.MainActivity$1.run(MainActivity.java:27)
at android.app.Activity.runOnUiThread(Activity.java:4794)
at com.example.androidyue.bitmapdemo.MainActivity.onResume(MainActivity.java:33)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1282)
at android.app.Activity.performResume(Activity.java:5405)
** BroadcastReceiver过了60秒居然没有ANR?**
public class NetworkReceiver extends BroadcastReceiver{
private static final String LOGTAG = "NetworkReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOGTAG, "onReceive intent=" + intent);
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(LOGTAG, "onReceive end");
}
}
实际上已经发生了ANR,只是没有进行对话框弹出而已。这种ANR就是background ANR,即后台程序的ANR,我们可以通过过滤日志验证
adb logcat | grep "NetworkReceiver|ActivityManager|WindowManager"
I/NetworkReceiver( 4109): onReceive intent=Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 cmp=com.example.androidyue.bitmapdemo/.NetworkReceiver (has extras) }
I/ActivityManager( 462): No longer want com.android.exchange (pid 1054): empty #17
I/NetworkReceiver( 4109): onReceive end
W/BroadcastQueue( 462): Receiver during timeout: ResolveInfo{5342dde4 com.example.androidyue.bitmapdemo.NetworkReceiver p=0 o=0 m=0x108000}
E/ActivityManager( 462): ANR in com.example.androidyue.bitmapdemo
E/ActivityManager( 462): Reason: Broadcast of Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 cmp=com.example.androidyue.bitmapdemo/.NetworkReceiver (has extras) }
E/ActivityManager( 462): Load: 0.37 / 0.2 / 0.14
E/ActivityManager( 462): CPU usage from 26047ms to 0ms ago:
E/ActivityManager( 462): 0.4% 58/adbd: 0% user + 0.4% kernel / faults: 1501 minor
E/ActivityManager( 462): 0.3% 462/system_server: 0.1% user + 0.1% kernel
E/ActivityManager( 462): 0% 4109/com.example.androidyue.bitmapdemo: 0% user + 0% kernel / faults: 6 minor
E/ActivityManager( 462): 1.5% TOTAL: 0.5% user + 0.9% kernel + 0% softirq
E/ActivityManager( 462): CPU usage from 87ms to 589ms later:
E/ActivityManager( 462): 1.8% 58/adbd: 0% user + 1.8% kernel / faults: 30 minor
E/ActivityManager( 462): 1.8% 58/adbd: 0% user + 1.8% kernel
E/ActivityManager( 462): 4% TOTAL: 0% user + 4% kernel
W/ActivityManager( 462): Killing ProcessRecord{5326d418 4109:com.example.androidyue.bitmapdemo/u0a10063}: background ANR
I/ActivityManager( 462): Process com.example.androidyue.bitmapdemo (pid 4109) has died.
可以更容易了解background ANR么?
当然可以,在Android开发者选项—>高级—>显示所有”应用程序无响应“勾选即可对后台ANR也进行弹窗显示,方便查看了解程序运行情况。