android 服务经常挂掉,Android AMS怎么监听异常挂掉的进程?

本文介绍了如何使用Android AMS来监听服务进程异常挂掉的情况。通过分析`ActivityThread`、`ActivityManagerService`和`IBinder`的`linkToDeath`方法,当服务的Binder对象死亡时,会触发`AppDeathRecipient`的`binderDied`方法,从而得知服务已挂掉。同时,还提到了`IProcessObserver`接口用于注册和接收进程状态变化的回调。

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

代码分析

1.代码路径

frameworks/base/core/java/android/app/ActivityThread.java

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

frameworks/base/core/java/android/os/IBinder.java

代码参考:Android Q

2.ActivityThread

public static void main(String[] args) {

······

ActivityThread thread = new ActivityThread();

thread.attach(false, startSeq);

······

}

private void attach(boolean system, long startSeq) {

······

final IActivityManager mgr = ActivityManager.getService();

try {

mgr.attachApplication(mAppThread, startSeq);

} catch (RemoteException ex) {

throw ex.rethrowFromSystemServer();

}

······

}

1.final ApplicationThread mAppThread = new ApplicationThread();

2.private class ApplicationThread extends IApplicationThread.Stub

3.ActivityManagerService.java

public final void attachApplication(IApplicationThread thread, long startSeq) {

······

attachApplicationLocked(thread, callingPid, callingUid, startSeq);

······

}

private final boolean attachApplicationLocked(IApplicationThread thread,

int pid, int callingUid, long startSeq) {

······

try {

AppDeathRecipient adr = new AppDeathRecipient(

app, pid, thread);

thread.asBinder().linkToDeath(adr, 0);

app.deathRecipient = adr;

} catch (RemoteException e) {

app.resetPackageList

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值