github网址:https://github.com/SalomonBrys/ANR-WatchDog
buld.gradle 中引入implementation 'com.github.anrwatchdog:anrwatchdog:1.4.0'
public class WatchDogUtils {
public static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 1001;
private static final String ADR_DUMP_PATH = "anrHprof";
private static File mExternalReportPath;
private static ANRWatchDog anrWatchDog;
public static void startAnrWatch(Context context) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
(Activity) context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
} else {
initANRWatchDog(context);
}
}
public static void closeANRWatchDog(Context context) {
}
public static void initANRWatchDog(Context context) {
if (mExternalReportPath == null) {
mExternalReportPath = new File(context.getFilesDir(), ADR_DUMP_PATH);
if (!mExternalReportPath.exists()) {
mExternalReportPath.mkdirs();
}
}
if (anrWatchDog == null) {
anrWatchDog = new ANRWatchDog();
}
anrWatchDog
.setANRListener(new ANRWatchDog.ANRListener() {
@Override
public void onAppNotResponding(ANRError error) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
String name = "anr" + formatter.format(date);
File anr = new File(mExternalReportPath, name);
try {
Debug.dumpHprofData(anr.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
})
.start();
}
}

本文介绍了如何通过ANR-WatchDog库来监控并捕获Android应用的错误日志。通过在build.gradle中引入该库,开发者可以更有效地调试和解决应用程序无响应(ANR)问题。
1026

被折叠的 条评论
为什么被折叠?



