• Android记录程序崩溃Log写入文件


    将导致程序崩溃的堆栈调用Log写入文件,便于收集bug。在调试安卓程序,由于某些原因调试时手机不能连接PC端,无法通过IDE查看程序崩溃的Log,希望log能够写入文件中,对于已经发布的App可以通过该功能收集Bug。

    01import java.io.FileNotFoundException;
    02import java.io.FileOutputStream;
    03import java.io.IOException;
    04import java.io.PrintStream;
    05import java.lang.Thread.UncaughtExceptionHandler;
    06 
    07public class MyCrashHandler implements UncaughtExceptionHandler{
    08 
    09    private static MyCrashHandler crashHandler;
    10     
    11    @Override
    12    public void uncaughtException(Thread thread, Throwable ex) {
    13        // TODO Auto-generated method stub
    14        if (crashHandler != null) {
    15            try {vi模板大全
    16                //将crash log写入文件
    17                FileOutputStream fileOutputStream = new FileOutputStream("/mnt/sdcard/crash_log.txt", true);
    18                PrintStream printStream = new PrintStream(fileOutputStream);
    19                ex.printStackTrace(printStream);
    20                printStream.flush();
    21                printStream.close();
    22                fileOutputStream.close();
    23            } catch (FileNotFoundException e) {
    24                // TODO Auto-generated catch block
    25                e.printStackTrace();
    26            } catch (IOException e) {
    27                // TODO Auto-generated catch block
    28                e.printStackTrace();
    29            }
    30        }
    31    } 
    32     
    33    //设置默认处理器
    34    public void init() {
    35        Thread.setDefaultUncaughtExceptionHandler(this);
    36    }http://www.huiyi8.com/vi/
    37     
    38    private MyCrashHandler() {}
    39     
    40    //单例
    41    public static MyCrashHandler instance() {
    42        if (crashHandler == null) {
    43            synchronized (crashHandler) {
    44                crashHandler = new MyCrashHandler();
    45            }
    46        }
    47        return crashHandler;
    48    }
    49}
  • 相关阅读:
    河北省重大技术需求征集系统(10)
    河北省重大技术需求征集系统(9)
    大三上学期周总结
    河北省重大技术需求征集系统(8)
    《代码大全》阅读笔记(三)
    河北省重大技术需求征集系统(7)
    河北省重大技术需求征集系统(6)
    河北省重大技术需求征集系统(5)
    【自动化】Aritest+python实现客户端UI自动化
    【自动化】Monkey自动化测试实现总结
  • 原文地址:https://www.cnblogs.com/xkzy/p/3808236.html
Copyright © 2020-2023  润新知