Android(安卓)崩溃日志的获取方式
16lz
2021-12-20
1.方式一:
Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { writeErrorLog(e); }};protected void writeErrorLog(Throwable throwable) { String info = null; ByteArrayOutputStream outputStream = null; PrintStream printStream = null; try { outputStream = new ByteArrayOutputStream(); printStream = new PrintStream(outputStream); throwable.printStackTrace(printStream); byte[] data = outputStream.toByteArray(); info = new String(data); data = null; } catch (Exception e) { e.printStackTrace(); } finally { try { if (printStream != null) { printStream.close(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } File dir = new File(Error_Log); if (dir.exists()) { dir.mkdir(); } File file = new File(dir, Log_Name); try { FileOutputStream fileOutputStream = new FileOutputStream(file, true); fileOutputStream.write(info.getBytes()); fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
2.方式二:
依赖它
compile 'com.github.wenmingvs:LogReport:1.0.3'
MyApplication中:
LogReport.getInstance() .setCacheSize(30 * 1024 * 1024)//支持设置缓存大小,超出后清空 .setLogDir(getApplicationContext(), Environment.getExternalStorageDirectory(). getAbsolutePath() + "/" + this.getString(this.getApplicationInfo().labelRes) + "/")//定义路径为:sdcard/[app name]/ .setWifiOnly(true)//设置只在Wifi状态下上传,设置为false为Wifi和移动网络都上传 .setLogSaver(new CrashWriter(getApplicationContext()))//支持自定义保存崩溃信息的样式 //.setEncryption(new AESEncode()) //支持日志到AES加密或者DES加密,默认不开启 .init(getApplicationContext());
更多相关文章
- Android(安卓)Studio学习:简单控件:滚动显示文本
- android典型代码系列(二十五)------popupwindow的使用
- Android之Camera预览
- android 时间对话框
- Android设置RadioButton在文字的右边
- android 权限
- Android(安卓)Path的使用
- android 实现拍照有2种方式
- Android6.0修改设备设置中kernel信息