/** * 代表的是当前应用程序的进程. */ public class MobliesafeApplication extends Application { public BlackNumberInfo info; @Override public void onCreate() { super.onCreate(); Thread.currentThread().setUncaughtExceptionHandler( new MyExceptionHandler());// 这样就能够让异常的处理器设置到我们的程序中 } public class MyExceptionHandler implements UncaughtExceptionHandler { private static final String TAG = "MyExceptionHandler"; @Override public void uncaughtException(Thread arg0, Throwable arg1) { Logger.i(TAG, "发生了异常"); try { Field[] fields = Build.class.getDeclaredFields();// 可以通过Build的属性来获取到手机的硬件信息,由于不同手机的硬件信息部一定有,所以要用反射得到 StringBuffer sb = new StringBuffer(); for (Field field : fields) { String info = field.getName() + ":" + field.get(null) + "\n"; sb.append(info); } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); arg1.printStackTrace(pw);// 通过这个来得到异常信息 String errorlog = sw.toString(); File file = new File(Environment.getExternalStorageDirectory(), "error.log"); FileOutputStream fos = new FileOutputStream(file); sb.append(errorlog); fos.write(sb.toString().getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } android.os.Process.killProcess(android.os.Process.myPid());// 这个是只能杀死自己不能杀死别人,这时候系统发现程序在自己的范围之内死了,系统就会重启程序到出现错误之前的那个Activity。 } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步