ANR 弹窗的显示原理,2021安卓面试题

应用并没有 ANR 弹窗,不过 logcat 有 ANR 相关信息,看一下 trace 文件:

----- pid 14608 at 2021-03-23 19:56:20 -----

Cmd line: demo.com.sam.demofactory:whatever

… 省略无关信息

“main” prio=5 tid=1 Sleeping

| group=“main” sCount=1 dsCount=0 flags=1 obj=0x73f13a80 self=0x78e7cc2a00

| sysTid=14608 nice=0 cgrp=default sched=0/0 handle=0x796c96d9a8

| state=S schedstat=( 57816925 3067496 80 ) utm=2 stm=3 core=4 HZ=100

| stack=0x7fe1d03000-0x7fe1d05000 stackSize=8MB

| held mutexes=

at java.lang.Thread.sleep(Native method)

  • sleeping on <0x0a71f3e8> (a java.lang.Object)

at java.lang.Thread.sleep(Thread.java:373)

  • locked <0x0a71f3e8> (a java.lang.Object)

at java.lang.Thread.sleep(Thread.java:314)

at demo.com.sam.demofactory.service.NoZuoNoDieService.onCreate(NoZuoNoDieService.kt:15)

at android.app.ActivityThread.handleCreateService(ActivityThread.java:3426)

at android.app.ActivityThread.-wrap4(ActivityThread.java:-1)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)

at android.os.Handler.dispatchMessage(Handler.java:106)

at android.os.Looper.loop(Looper.java:171)

at android.app.ActivityThread.main(ActivityThread.java:6611)

at java.lang.reflect.Method.invoke(Native method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

ANR 弹窗的显示原理

===========

我们来看一下这个没有弹窗的 ANR 是怎么发生的

首先来复习一下 说一说 Service 的启动流程

ANR 弹窗的显示原理,2021安卓面试题

AMS 真正去启动 Service 调用的是 ActiveServices.realStartServiceLocked 方法:

// API 29 com.android.server.am.ActiveServices

private final void realStartServiceLocked(ServiceRecord r,

ProcessRecord app, boolean execInFg) throws RemoteException {

bumpServiceExecutingLocked(r, execInFg, “create”);

}

private final void bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why) {

scheduleServiceTimeoutLocked(r.app);

}

// 通过 Handler 延时发一条消息,延时时间则为 Service 触发的 ANR 时长

// SERVICE_TIMEOUT = 20*1000

// SERVICE_BACKGROUND_TIMEOUT = SERVICE_TIMEOUT * 10;

void scheduleServiceTimeoutLocked(ProcessRecord proc) {

if (proc.executingServices.size() == 0 || proc.thread == null) {

return;

}

Message msg = mAm.mHandler.obtainMessage(

ActivityManagerService.SERVICE_TIMEOUT_MSG);

msg.obj = proc;

mAm.mHandler.sendMessageDelayed(msg,

proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT);

}

如果 Service 在规定时间内启动完成,则这个消息会被 remove 掉,我们今天要看一下超时之后,收到这个消息是怎么处理的。

// com.android.server.am.ActivityManagerService.MainHandler

final class MainHandler extends Handler {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case SERVICE_TIMEOUT_MSG: {

mServices.serviceTimeout((ProcessRecord)msg.obj);

} break;

}

}

mServices 是 ActiveServices:

// com.android.server.am.ActiveServices#serviceTimeout

void serviceTimeout(ProcessRecord proc) {

if (timeout != null && mAm.mProcessList.mLruProcesses.contains(proc)) {

Slog.w(TAG, "Timeout executing service: " + timeout);

mAm.mHandler.removeCallbacks(mLastAnrDumpClearer);

mAm.mHandler.postDelayed(mLastAnrDumpClearer, LAST_ANR_LIFETIME_DURATION_MSECS);

anrMessage = "executing service " + timeout.shortInstanceName;

}

if (anrMessage != null) {

proc.appNotResponding(null, null, null, null, false, anrMessage);

}

}

appNotResponding 就是最终处理 ANR 逻辑的代码了

// com.android.server.am.ProcessRecord

void appNotResponding(String activityShortComponentName,

ApplicationInfo aInfo,

…) {

if (isMonitorCpuUsage()) {

mService.updateCANR 弹窗的显示原理,2021安卓面试题
puStatsNow();

}

if (isSilentAnr() && !isDebugging()) {

kill(“bg anr”, true);

return;

}

// Bring up the infamous App Not Responding dialog

Message msg = Message.obtain();

msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;

msg.obj = new AppNotRespondingDialog.Data(this, aInfo, aboveSystem);

mService.mUiHandler.sendMessage(msg);

}

如果 isSilentAnr() 为 true,则直接杀死所在进程,不会发送显示 ANR 弹窗的消息。来看下相关方法

boolean isSilentAnr() {

return !getShowBackground() && !isInterestingForBackgroundTraces();

}

private boolean getShowBackground() {
);

mService.mUiHandler.sendMessage(msg);

}

如果 isSilentAnr() 为 true,则直接杀死所在进程,不会发送显示 ANR 弹窗的消息。来看下相关方法

boolean isSilentAnr() {

return !getShowBackground() && !isInterestingForBackgroundTraces();

}

private boolean getShowBackground() {

上一篇:不一样的冒泡排序,通俗易懂


下一篇:如何在3分钟内在Joomla中添加规范标签