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

Commit

Permalink
进程是否ANR的判定使用一种更加准确和高效的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
aceding committed Mar 30, 2021
1 parent e3bd0ea commit 80d2829
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
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

0 comments on commit 80d2829

Please sign in to comment.