anr使用方法是什么
在Android上,如果你的 應(yīng)用程序有一段時(shí)間響應(yīng)不夠靈敏,系統(tǒng)會(huì)向用戶顯示一個(gè)對(duì)話框,這個(gè)對(duì)話框稱作應(yīng)用程序無響應(yīng)(ANR:Application Not Responding)對(duì)話框。那么,anr 使用方法是什么?
anr如何定位
如果開發(fā)機(jī)器上出現(xiàn)問題,我們可以通過查看/data/anr/traces.txt即可,最新的ANR信息在最開始部分。我們從stacktrace中即可找到出問題的具體行數(shù)。本例中問題出現(xiàn)在MainActivity.java 27行,因?yàn)檫@里調(diào)用了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 class="main">
anr使用方法是什么
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)
如果是線上版本引起的,Google Play后臺(tái)有相關(guān)的數(shù)據(jù)可以幫助查看分析并解決問題。
anr細(xì)致分析
提問: BroadcastReceiver過了60秒居然沒有ANR? 現(xiàn)場(chǎng)代碼如下
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");
}
}
回答:實(shí)際上已經(jīng)發(fā)生了ANR,只是沒有進(jìn)行對(duì)話框彈出而已。這種ANR就是background ANR,即后臺(tái)程序的ANR,我們可以通過過濾日志驗(yàn)證
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.
除了日志,我們還可以根據(jù)前面提到的查看traces.txt文件。