diff --git a/xcrash_lib/src/main/java/xcrash/AnrHandler.java b/xcrash_lib/src/main/java/xcrash/AnrHandler.java index 1d34dab..895eeeb 100644 --- a/xcrash_lib/src/main/java/xcrash/AnrHandler.java +++ b/xcrash_lib/src/main/java/xcrash/AnrHandler.java @@ -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; } } diff --git a/xcrash_lib/src/main/java/xcrash/NativeHandler.java b/xcrash_lib/src/main/java/xcrash/NativeHandler.java index 6911afa..41f0ea0 100644 --- a/xcrash_lib/src/main/java/xcrash/NativeHandler.java +++ b/xcrash_lib/src/main/java/xcrash/NativeHandler.java @@ -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 } diff --git a/xcrash_lib/src/main/java/xcrash/Util.java b/xcrash_lib/src/main/java/xcrash/Util.java index 65176e2..7af03d2 100644 --- a/xcrash_lib/src/main/java/xcrash/Util.java +++ b/xcrash_lib/src/main/java/xcrash/Util.java @@ -43,6 +43,8 @@ import java.util.List; import java.util.Locale; +import static android.content.Context.ACTIVITY_SERVICE; + class Util { private Util() { @@ -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 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 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 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) {