對于runtime exception引起的crash
eg. int i = 100/0; 引起的java.lang.ArithmeticException
在/data/data/com.qihoo.browser下創(chuàng)建app_crash目錄, 并創(chuàng)建一個*.stacktrace文件,以json格式保存調用棧.
code 實現(xiàn):
ChromeApplication.java
->
public class CrashHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
//通過e創(chuàng)建一個CrashMessage對象, 之后創(chuàng)建.stacktrace文件, 保存調用棧信息.
CrashUploadManager.save(new CrashMessage(t, e, sProcessType, CrashMessageExtra.get()));
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
public static void init() {
Thread.setDefaultUncaughtExceptionHandler(
new CrashHandler(Thread.getDefaultUncaughtExceptionHandler()));
}
}
log中搜tag: Process 來定位crash的位置:
Process I Sending signal. PID: 21253 SIG: 9
對于caught exception态鳖,模擬一個IOException
try {
FileInputStream fis = new FileInputStream(“/data/data/com.qihoo.browser/files/DecRawsoLib/libchrome_public2.so”);
fis.read();
} catch (IOException e) {
e.printStackTrace();
BLog.e("ahking", "FileInputStream read()", e);
}
調用棧的內容會追加到caught_crashes.txt的末尾.
/storage/emulated/legacy/360browser/log/caught_crashes.txt
code 實現(xiàn):
public class BLog {
public static final String CAUGHTEXCEPTIONPATH = CRASH_FILE_DIR_PATH + "caught_crashes.txt";
public static int e(String tag, String msg, Throwable tr) {
//把時間信息, 設備信息, 連同exception的stacktrace內容, 追加到"caught_crashes.txt"文件的末尾.
saveLogInfoToFile(CAUGHTEXCEPTIONPATH, msg, tr);
return android.util.Log.e(tag, msg, tr);
}
}
良好的編碼習慣是在每個try{}catch(Exception e){}中根时, 都調用BLog自定義的e(String tag, String msg, Throwable tr)把異常信息保存到本地漂佩,方便記錄異常情況.
---DONE---