仅仅做了简单的保存到了本地而已;
根据需要可以继续增加功能:
下一次启动上传到服务器;
增加应用版本,机型系统版本信息等;
public class CrashSaver { public static CrashSaver crashSaver; private Context mContext; private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler; public CrashSaver(Context context) { mContext = context; mUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { saveLog(ex); mUncaughtExceptionHandler.uncaughtException(thread, ex); } }); } private void saveLog(Throwable ex) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timeStamp = sdf.format(new Date(System.currentTimeMillis())); File file = new File(Environment.getExternalStorageDirectory() + File.separator + "CrashLog" + File.separator + timeStamp + ".txt"); if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); try { StackTraceElement[] stes = ex.getStackTrace(); StringBuilder sb = new StringBuilder(""); for (StackTraceElement ste : stes) sb.append(ste.toString() + "\n"); FileWriter writer = new FileWriter(file); writer.write(sb.toString()); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } // 应用启动时调用一次即可 public static void init(Context context) { if (crashSaver == null) crashSaver = new CrashSaver(context); } }
时间: 2024-12-28 13:22:43