Android 获取手机实时崩溃日志

1.创建 MyApplication extends Application,然后在AndroidManifest.xml文件中的application标签下配置name属性,指向这个application;

2. 在MyApplication中重写OnCreate()方法添加代码,如下图:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //记录崩溃信息
        final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
                //获取崩溃时的UNIX时间戳
                long timeMillis = System.currentTimeMillis();
                //将时间戳格式化,建立一个String拼接器
                StringBuilder stringBuilder = new StringBuilder(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(timeMillis)));
                stringBuilder.append(":\n");
                //获取错误信息
                stringBuilder.append(throwable.getMessage());
                stringBuilder.append("\n");
                //获取堆栈信息
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                throwable.printStackTrace(pw);
                stringBuilder.append(sw.toString());

                //这就是完整的错误信息了,你可以拿来上传服务器,或者做成本地文件保存等等等等
                String errorLog = stringBuilder.toString();
                Log.e("测试", errorLog);
                //把获取到的日志写到本地,ErrorText.txt文件名字可以自己定义
                File rootFile = getRootFile(MyApplication.this);
                Log.e("测试", rootFile.toString());
                File file = new File(rootFile, "ErrorText.txt");
                BufferedWriter bufferedWriter = null;
                try {
                    //写入数据
                    bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                    bufferedWriter.write(errorLog + "\r\n");
                    bufferedWriter.flush();
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                } finally {
                    try {
                        if (null != bufferedWriter) {
                            bufferedWriter.close();
                        }
                    } catch (IOException e) {
                    }
                }
                //最后如何处理这个崩溃,这里使用默认的处理方式让APP停止运行
                defaultHandler.uncaughtException(thread, throwable);
            }
        });
    }
 /**
     * 安卓存储目录
     */
    public static File getRootFile(Application application) {
        //判断根目录
        File rootFile = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            //Android Q 系统私有空间创建
            ///storage/emulated/0/Android/data/<package name>/files/ -- 应用卸载会删除该目录下所有文件
            rootFile = application.getExternalFilesDir("");
        } else {
            //应用专属目录,位置/data/data//files
            rootFile = application.getFilesDir();
        }
        return rootFile;
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值