- UID
- 872339
|
将导致程序崩溃的堆栈调用Log写入文件,便于收集bug。在调试安卓程序,由于某些原因调试时手机不能连接PC端,无法通过IDE查看程序崩溃的Log,希望log能够写入文件中,对于已经发布的App可以通过该功能收集Bug。
01 | import java.io.FileNotFoundException; |
02 | import java.io.FileOutputStream; |
03 | import java.io.IOException; |
04 | import java.io.PrintStream; |
05 | import java.lang.Thread.UncaughtExceptionHandler; |
07 | public class MyCrashHandler implements UncaughtExceptionHandler{ |
09 | private static MyCrashHandler crashHandler; |
12 | public void uncaughtException(Thread thread, Throwable ex) { |
13 | // TODO Auto-generated method stub |
14 | if (crashHandler != null) { |
17 | FileOutputStream fileOutputStream = new FileOutputStream("/mnt/sdcard/crash_log.txt", true); |
18 | PrintStream printStream = new PrintStream(fileOutputStream); |
19 | ex.printStackTrace(printStream); |
22 | fileOutputStream.close(); |
23 | } catch (FileNotFoundException e) { |
24 | // TODO Auto-generated catch block |
26 | } catch (IOException e) { |
27 | // TODO Auto-generated catch block |
35 | Thread.setDefaultUncaughtExceptionHandler(this); |
38 | private MyCrashHandler() {} |
41 | public static MyCrashHandler instance() { |
42 | if (crashHandler == null) { |
43 | synchronized (crashHandler) { |
44 | crashHandler = new MyCrashHandler(); |
|
|