android watch 开发,Android应用开发Android Watchdog 机制概述

本文详细介绍了Android应用开发中的Watchdog机制,通过两张图和代码片段展示了其工作原理。当监测到应用出现死锁或者超时情况时,Watchdog会进行堆栈跟踪并可能执行重启操作。这一机制对于确保应用稳定性和性能至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文将带你了解Android应用开发Android Watchdog 机制概述,希望本文对大家学Android有所帮助。

a0f09987a921e21759975d35b08f4318.png

用两张图和部分代码来描述:

第一张图(图片需要在新标签中打开):

第二张图

代码 Watchdog.run():

@Override

public void run() {

boolean waitedHalf = false;

while (true) {

...

synchronized (this) {

long timeout =   CHECK_INTERVAL;

// Make sure we (re)spin   the checkers that have become idle within

// this wait-and-check   interval

for (int i=0;   i0)   {

if   (Debug.isDebuggerConnected()) {

debuggerWasConnected = 2;

}

try {

wait(timeout);

} catch   (InterruptedException e) {

Log.wtf(TAG,   e);

}

if   (Debug.isDebuggerConnected()) {

debuggerWasConnected   = 2;

}

timeout =   CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);

}

final int waitState =   evaluateCheckerCompletionLocked();

if (waitState == COMPLETED)   {

// The monitors have   returned; reset

waitedHalf =   false;

continue;

} else if (waitState ==   WAITING) {

// still waiting but   within their configured intervals; back off and recheck

continue;

} else if (waitState ==   WAITED_HALF) {

if (!waitedHalf)   {

// We've waited   half the deadlock-detection interval.    Pull a stack

// trace and wait   another half.

ArrayListpids = new ArrayList();

pids.add(Process.myPid());

ActivityManagerService.dumpStackTraces(true, pids, null, null,

NATIVE_STACKS_OF_INTEREST);

waitedHalf =   true;

}

continue;

}

// something is   overdue!

blockedCheckers =   getBlockedCheckersLocked();

subject =   describeCheckersLocked(blockedCheckers);

allowRestart =   mAllowRestart;

}

//后面的逻辑就是 dump   trace和重启了

...

HandlerChecker.scheduleCheckLocked():

HandlerChecker.run():

HandlerChecker.getCompletionStateLocked():

Watchdog.evaluateCheckerCompletionLocked():

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值