? ? 用來處理android崩潰日志收集的代碼义郑,詳情的使用請(qǐng)轉(zhuǎn):android崩潰日志收集和處理
第一個(gè)類
/**
* 異常捕捉實(shí)現(xiàn)類
*/
public class ErrorCaughtimplements Thread.UncaughtExceptionHandler {
private ErrorHandleerrHandle;
//設(shè)置本程序的異常崩潰由此類處理
? ? public ErrorCaught(Application context){
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
errHandle =new ErrorHandle(context , uncaughtExceptionHandler);
Thread.setDefaultUncaughtExceptionHandler(this);
}
//異常崩潰發(fā)生時(shí)調(diào)用的方法牧抽,這里面開始我們想要的操作,包括日志的手機(jī)和上傳等
? ? @Override
? ? public void uncaughtException(Thread thread, Throwable throwable) {
errHandle.excute(thread, throwable);
}
}
第二個(gè)類
/**
* 異常具體處理類
*/
public class ErrorHandle {
private Contextcontext;
private Thread.UncaughtExceptionHandleruncaughtExceptionHandler;
private FilecrashFile;
//新建的時(shí)候陈惰,隨即的開始建造崩潰文件夾和崩潰文件
? ? 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();//創(chuàng)建崩潰捕捉所在文件夾
? ? ? ? ? ? }
crashFile =new File(file, getCrashFileName());
if (!crashFile.exists()) {
try {
crashFile.createNewFile();//創(chuàng)建崩潰捕捉文件
? ? ? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
}
}
}
}
//用來執(zhí)行崩潰時(shí)具體的操作
? ? public void excute(Thread thread, Throwable throwable) {
CrashInforMationDetail crashInforMationDetail = CrashInforMationDetail.produce(throwable, thread,context);
crashInforMationDetail.writeToFile(crashFile);
signOut(thread, throwable);
}
//強(qiáng)制退出軟件
? ? 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();
}
}
第三個(gè)類
/**
* 獲取崩潰日志的具體內(nèi)容毕籽,這里做了兩個(gè)處理抬闯,一個(gè)是錯(cuò)誤信息輸入文件,一個(gè)是把錯(cuò)誤信息變成字符串
*/
public class CrashInforMationDetail {
private static StringcrashInfor;//崩潰日志的具體內(nèi)容
? ? private CrashInforMationDetail() {
}
//獲取錯(cuò)誤等信息
? ? 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();
}
//把錯(cuò)誤信息填充進(jìn)崩潰文件中
? ? 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();
}
}
}
//獲取手機(jī)的一些設(shè)備參數(shù)
? ? public static String getSysytemInfor() {
StringBuffer sb =new StringBuffer();
sb.append("主板:" + Build.BOARD +"\n");
sb.append("系統(tǒng)啟動(dòng)程序版本號(hào):" + Build.BOOTLOADER +"\n");
sb.append("系統(tǒng)定制商:" + Build.BRAND +"\n");
sb.append("cpu指令集:" + Build.CPU_ABI +"\n");
sb.append("cpu指令集2:" + Build.CPU_ABI2 +"\n");
sb.append("設(shè)置參數(shù):" + Build.DEVICE +"\n");
sb.append("顯示屏參數(shù):" + Build.DISPLAY +"\n");
sb.append("無線電固件版本:" + Build.getRadioVersion() +"\n");
sb.append("硬件識(shí)別碼:" + 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("硬件序列號(hào):" + Build.SERIAL +"\n");
sb.append("手機(jī)制造商:" + Build.PRODUCT +"\n");
sb.append("描述Build的標(biāo)簽:" + 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;
}
}