參考:https://www.cnblogs.com/android-blogs/p/5718302.html
定義
Application no responding 引用程序無(wú)相應(yīng)。表象:彈出一個(gè)無(wú)響應(yīng)彈窗提示盛撑,提供繼續(xù)等待按鈕和強(qiáng)行關(guān)閉按鈕
觸發(fā)時(shí)機(jī)
1. InputDispatching/KeyDispatching TimeOut
//時(shí)間
KEY_DISPATCHING_TIMEOUT = 5 *1000;
//調(diào)用開(kāi)始在native層锌仅,后轉(zhuǎn)到j(luò)ava層,調(diào)用鏈如下
InputManagerService.notifyANR
InputMonitor.notifyANR
AMP.inputDispatchingTimedOut
AMS.inputDispatchingTimedOut
//最后走的是
mHandler.post(new Runnable() {
public void run() {
appNotResponding(proc, activity, parent, aboveSystem, annotation);
}
});
2. BroadcastQueue TimeOut
//超時(shí)時(shí)間規(guī)定墙贱,在ActivityThread中定義
static final int BROADCAST_FG_TIMEOUT = 10*1000;
static final int BROADCAST_BG_TIMEOUT = 60*1000;
//在處理廣播前會(huì)發(fā)送一個(gè)廣播超時(shí)的消息
long timeoutTime = r.receiverTime + mTimeoutPeriod;
Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);
mHandler.sendMessageAtTime(msg, timeoutTime);
//處理完一個(gè)后呢會(huì)取消這個(gè)消息
mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this);
//到到了消息的響應(yīng)時(shí)間,如果還有沒(méi)處理完的消息贱傀,則觸發(fā)anr
if (anrMessage != null) {
mHandler.post(new AppNotResponding(app, anrMessage));
}
3. Service TimeOut
//超時(shí)時(shí)間的規(guī)定惨撇,在ActiveServices中定義
static final int SERVICE_TIMEOUT = 20*1000;
static final int SERVICE_BACKGROUND_TIMEOUT = SERVICE_TIMEOUT * 10;
//在Service的啟動(dòng)過(guò)程中,會(huì)在ActiveServices的realStartServiceLocked先Delay發(fā)送一條延時(shí)消息
mAm.mHandler.sendMessageAtTime(msg,
proc.execServicesFg ? (now+SERVICE_TIMEOUT) : (now+ SERVICE_BACKGROUND_TIMEOUT));
//然后執(zhí)行Service的創(chuàng)建和啟動(dòng)
app.thread.scheduleCreateService(r, r.serviceInfo,
mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo),
app.repProcState);
//創(chuàng)建啟動(dòng)完成會(huì)移除SERVICE_TIMEOUT_MSG這個(gè)消息
//當(dāng)前服務(wù)所在進(jìn)程中沒(méi)有正在執(zhí)行的service府寒,會(huì)移除SERVICE_TIMEOUT_MSG這個(gè)消息
mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app);
//如果在規(guī)定時(shí)間內(nèi)沒(méi)有移除SERVICE_TIMEOUT_MSG魁衙,則在handler的handleMessage中會(huì)觸發(fā)timeout,引起Anr
mAm.appNotResponding(proc, null, null, false, anrMessage);
4. ContentProvider TimeOut
//在ContentProviderClient中在執(zhí)行CURD等方法時(shí)會(huì)先調(diào)用如下方法
//這個(gè)方法就是延時(shí)執(zhí)行一個(gè)timeout后的任務(wù)對(duì)象株搔,也就是如果CURD的時(shí)間在這個(gè)時(shí)間內(nèi)完成將會(huì)觸發(fā)anr剖淀,這個(gè)時(shí)間是20s
private void beforeRemote() {
if (mAnrRunnable != null) {
sAnrHandler.postDelayed(mAnrRunnable, mAnrTimeout);
}
}
//可以看到任務(wù)對(duì)象調(diào)用的是appNotRespondingViaProvider
private class NotRespondingRunnable implements Runnable {
@Override
public void run() {
Log.w(TAG, "Detected provider not responding: " + mContentProvider);
mContentResolver.appNotRespondingViaProvider(mContentProvider);
}
}
//而上面最終觸發(fā)AMS.appNotRespondingViaProvider,來(lái)實(shí)現(xiàn)anr彈窗
mHandler.post(new Runnable() {
@Override
public void run() {
mAppErrors.appNotResponding(host, null, null, false,
"ContentProvider not responding");
}
});
mAppErrors.appNotResponding的流程
整理CPU使用情況纤房,CPU負(fù)載等信息纵隔,到trace文件,最后發(fā)送一個(gè)彈出anr彈窗的消息mService.mUiHandler.sendMessage(msg);最終在AppErrors中創(chuàng)建個(gè)AppNotRespondingDialog顯示
檢查策略
- 出現(xiàn)anr后炮姨,通過(guò)adb shell-->cat /data/anr/traces.txt 或 adb shell "cat /data/anr/traces.txt" | less命令可以查看anr的詳細(xì)信息
- 通過(guò)TraceView進(jìn)行分析:
- 選中想觀察的進(jìn)程捌刮。點(diǎn)擊Start Method Profiling,一通操作后舒岸,再點(diǎn)擊Stop method Profiling绅作。就是彈出這段時(shí)間操作的trace文件。
- 按Incl Real Time倒序排列
- 一類是找執(zhí)行時(shí)間久的方法蛾派,一類是找到調(diào)用次數(shù)多到不符合常理的方法俄认。
避免方案
- UI線程盡量只做跟UI相關(guān)的工作
- 耗時(shí)的工作()比如數(shù)據(jù)庫(kù)操作,I/O洪乍,網(wǎng)絡(luò)操作)眯杏,采用單獨(dú)的工作線程處理
- 用Handler來(lái)處理UIthread和工作thread的交互