前言
Android 11 引入了 ActivityManager.getHistoricalProcessExitReasons() 方法,用于报告近期任何进程终止的原因。应用可以使用此方法收集崩溃诊断信息,例如进程终止是由于 ANR、内存问题还是其他原因所致。
一、getHistoricalProcessExitReasons应用进程退出原因
经过一份测试后,然后对照官方文档,仔细阅读后,发现getHistoricalProcessExitReasons这个方法是获取历史进程退出原因的方法。
注意:系统将此历史信息存储在环形缓冲区中,只会返回最近的记录。
packageName: String, 包名
pid: Int, (A process ID that used to belong to this package but died later; a value of 0 means to ignore this parameter and return all matching records. Value is 0 or greater)
maxNum: Int 异常的数量
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List exitInfoList = activityManager.getHistoricalProcessExitReasons(getPackageName(),
0, 5);
注意:某些设备无法报告内存不足终止事件。在这些设备上,getHistoricalProcessExitReasons() 方法会返回 REASON_SIGNALED 而不是 REASON_LOW_MEMORY,并且 getStatus() 的返回值为 SIGKILL。 如需检查设备是否可以报告内存不足终止事件,请调用 ActivityManager.isLowMemoryKillReportSupported()。