Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

进程是否ANR的判定使用一种更加准确和高效的方法 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xcrash_lib/src/main/java/xcrash/AnrHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void handleAnr(String filepath) {

//check process error state
if (this.checkProcessState) {
if (!Util.checkProcessAnrState(this.ctx, anrTimeoutMs)) {
if (!Util.isProcessNotResponding(this.ctx)) {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion xcrash_lib/src/main/java/xcrash/NativeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private static void traceCallback(String logPath, String emergency) {

//check process ANR state
if (NativeHandler.getInstance().anrCheckProcessState) {
if (!Util.checkProcessAnrState(NativeHandler.getInstance().ctx, NativeHandler.getInstance().anrTimeoutMs)) {
if (!Util.isProcessNotResponding(NativeHandler.getInstance().ctx)) {
FileManager.getInstance().recycleLogFile(new File(logPath));
return; //not an ANR
}
Expand Down
49 changes: 30 additions & 19 deletions xcrash_lib/src/main/java/xcrash/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.List;
import java.util.Locale;

import static android.content.Context.ACTIVITY_SERVICE;

class Util {

private Util() {
Expand Down Expand Up @@ -240,29 +242,38 @@ static boolean checkAndCreateDir(String path) {
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
static boolean checkProcessAnrState(Context ctx, long timeoutMs) {
ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
if (am == null) return false;

static boolean isProcessNotResponding(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
if (null == am) {
return false;
}
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
if (null == runningAppProcesses) {
return false;
}
int pid = android.os.Process.myPid();
long poll = timeoutMs / 500;
for (int i = 0; i < poll; i++) {
List<ActivityManager.ProcessErrorStateInfo> processErrorList = am.getProcessesInErrorState();
if (processErrorList != null) {
for (ActivityManager.ProcessErrorStateInfo errorStateInfo : processErrorList) {
if (errorStateInfo.pid == pid && errorStateInfo.condition == ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING) {
return true;
}
}
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
if (null == runningAppProcessInfo) {
continue;
}

try {
Thread.sleep(500);
} catch (Exception ignored) {
if (pid == runningAppProcessInfo.pid) {
return false;
}
}

return false;
List<ActivityManager.ProcessErrorStateInfo> processesErrorState = am.getProcessesInErrorState();
if (null == processesErrorState) {
return false;
}
for (ActivityManager.ProcessErrorStateInfo processErrorStateInfo : processesErrorState) {
if (null == processErrorStateInfo) {
continue;
}
if (pid != processErrorStateInfo.pid) {
continue;
}
return processErrorStateInfo.condition != ActivityManager.ProcessErrorStateInfo.CRASHED;
}
return true;
}

static String getLogHeader(Date startTime, Date crashTime, String crashType, String appId, String appVersion) {
Expand Down