代码:android崩溃日志收集和处理
生活随笔
收集整理的這篇文章主要介紹了
代码:android崩溃日志收集和处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 用來處理android崩潰日志收集的代碼,詳情的使用請轉:android崩潰日志收集和處理
第一個類
/**
* 異常捕捉實現類
*/
public class ErrorCaughtimplements Thread.UncaughtExceptionHandler { private ErrorHandleerrHandle; //設置本程序的異常崩潰由此類處理public ErrorCaught(Application context){ Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); errHandle =new ErrorHandle(context , uncaughtExceptionHandler); Thread.setDefaultUncaughtExceptionHandler(this); } //異常崩潰發生時調用的方法,這里面開始我們想要的操作,包括日志的手機和上傳等@Overridepublic void uncaughtException(Thread thread, Throwable throwable) { errHandle.excute(thread, throwable); } } 復制代碼第二個類
/**
* 異常具體處理類
*/
public class ErrorHandle { private Contextcontext; private Thread.UncaughtExceptionHandleruncaughtExceptionHandler; private FilecrashFile; //新建的時候,隨即的開始建造崩潰文件夾和崩潰文件public ErrorHandle(Application context, Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { this.context = context; this.uncaughtExceptionHandler = uncaughtExceptionHandler; if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { File file =new File(Environment.getExternalStorageDirectory(),"crashCollection"); if (!file.exists()) { file.mkdirs();//創建崩潰捕捉所在文件夾} crashFile =new File(file, getCrashFileName()); if (!crashFile.exists()) { try { crashFile.createNewFile();//創建崩潰捕捉文件}catch (IOException e) { e.printStackTrace(); } } } } //用來執行崩潰時具體的操作public void excute(Thread thread, Throwable throwable) { CrashInforMationDetail crashInforMationDetail = CrashInforMationDetail.produce(throwable, thread,context); crashInforMationDetail.writeToFile(crashFile); signOut(thread, throwable); } //強制退出軟件public void signOut(Thread thread, Throwable throwable) { if (uncaughtExceptionHandler !=null) { uncaughtExceptionHandler.uncaughtException(thread, throwable); }else { android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } } //獲取崩潰文件名稱,具體是年月日組成的文件名private String getCrashFileName() { StringBuilder stringBuilder =new StringBuilder(); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int date = calendar.get(Calendar.DATE); stringBuilder.append("crash_"); stringBuilder.append(year +"-"); stringBuilder.append(month +"-"); stringBuilder.append(date); stringBuilder.append(".txt"); return stringBuilder.toString(); } } 復制代碼第三個類
/**
* 獲取崩潰日志的具體內容,這里做了兩個處理,一個是錯誤信息輸入文件,一個是把錯誤信息變成字符串
*/
public class CrashInforMationDetail { private static StringcrashInfor;//崩潰日志的具體內容private CrashInforMationDetail() { } //獲取錯誤等信息public static CrashInforMationDetail produce(Throwable throwable, Thread thread, Context context) { ByteArrayOutputStream out =new ByteArrayOutputStream(); PrintStream print =new PrintStream(out); out.toString(); print.append("crahtime:" + System.currentTimeMillis()).append("\n"); print.append(getSysytemInfor()); print.append("theadName:" + thread.getName() +"\n").append("threadID:" + thread.getId() +"\n"); crashInfor =getSysytemInfor() +"threadName:" + thread.getName() +"\n" +"threadID:" + thread.getId() +"\n" +"ErrorInformation:" + throwable.getMessage(); print.append(throwable.getMessage()).append("\n"); StackTraceElement[] stackTrace = throwable.getStackTrace(); try { for (int i =0; i < stackTrace.length; i++) { StackTraceElement stackTraceElement = stackTrace[i]; String trace = stackTraceElement.toString(); print.append(trace +"\n"); crashInfor += trace +"\n"; } }catch (Exception e) { e.printStackTrace(); } Log.d("crashInfor",crashInfor); throwable.printStackTrace(print); return new CrashInforMationDetail(); } //把錯誤信息填充進崩潰文件中public void writeToFile(File file) { PrintWriter printer =null; try { BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file,true)); printer =new PrintWriter(out); printer.println(crashInfor); printer.flush(); }catch (IOException e) { e.printStackTrace(); }finally { if (printer !=null) { printer.close(); } } } //獲取手機的一些設備參數public static String getSysytemInfor() { StringBuffer sb =new StringBuffer(); sb.append("主板:" + Build.BOARD +"\n"); sb.append("系統啟動程序版本號:" + Build.BOOTLOADER +"\n"); sb.append("系統定制商:" + Build.BRAND +"\n"); sb.append("cpu指令集:" + Build.CPU_ABI +"\n"); sb.append("cpu指令集2:" + Build.CPU_ABI2 +"\n"); sb.append("設置參數:" + Build.DEVICE +"\n"); sb.append("顯示屏參數:" + Build.DISPLAY +"\n"); sb.append("無線電固件版本:" + Build.getRadioVersion() +"\n"); sb.append("硬件識別碼:" + Build.FINGERPRINT +"\n"); sb.append("硬件名稱:" + Build.HARDWARE +"\n"); sb.append("HOST:" + Build.HOST +"\n"); sb.append("修訂版本列表:" + Build.ID +"\n"); sb.append("硬件制造商:" + Build.MANUFACTURER +"\n"); sb.append("版本:" + Build.MODEL +"\n"); sb.append("硬件序列號:" + Build.SERIAL +"\n"); sb.append("手機制造商:" + Build.PRODUCT +"\n"); sb.append("描述Build的標簽:" + Build.TAGS +"\n"); sb.append("TIME:" + Build.TIME +"\n"); sb.append("builder類型:" + Build.TYPE +"\n"); sb.append("USER:" + Build.USER +"\n"); return sb.toString(); } public String getString() { return crashInfor; } }復制代碼轉載于:https://juejin.im/post/5a5419a3518825732a6d5bca
總結
以上是生活随笔為你收集整理的代码:android崩溃日志收集和处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 本地yum原的制作
- 下一篇: 记一次意外的自定义控件