Android如何自動記錄異常信息至SD卡:
public class MyCrashHandler implements UncaughtExceptionHandler {
@SuppressWarnings("unused")
private Context context;
private static MyCrashHandler crashHandler = new MyCrashHandler();
UncaughtExceptionHandler defaultExceptionHandler;
private MyCrashHandler() {
}
public static MyCrashHandler getInstanceMyCrashHandler() {
return crashHandler;
}
/**
* 初始化方法
* @param context 上下文對象
*/
public void init(Context context) {
this.context = context;
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* 異常處理方法
* @Params Thread對象
* @param Throwable對象
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(thread, ex) && defaultExceptionHandler != null) {
defaultExceptionHandler.uncaughtException(thread, ex);
}
}
// 程序異常處理方法
private boolean handleException(Thread thread, Throwable ex) {
StringBuilder sb = new StringBuilder();
long startTimer = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss ");
Date firstDate = new Date(System.currentTimeMillis()); // 第一次創建文件,也就是開始日期
String str = formatter.format(firstDate);
sb.append(startTimer);
//sb.append("\n");
sb.append(str); // 把當前的日期寫入到字符串中
Writer writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
ex.printStackTrace(pw);
String errorresult = writer.toString();
sb.append(errorresult);
sb.append("\n");
try {
File fileDir = new File("/data/data/com.ebank/Ebank/");
//File fileDir = new File("/sdcard/com.ebank/EBank/");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File files = new File(fileDir, "ebank.log");
if (!files.exists()) {
files.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(files,
true);
fileOutputStream.write(sb.toString().getBytes());
fileOutputStream.close();
// 文件大小限制在1M,超過1M自動刪除
FileInputStream fileInputStream = new FileInputStream(files);
int sizeK = fileInputStream.available() / 1024; // 單位是KB
int totalSize = 1 * 1024;
if (sizeK > totalSize) {
boolean b = files.delete();
if (b) { // 刪除成功,重新創建一個文件
@SuppressWarnings("unused")
File filesTwo = new File(fileDir, "ebank.log");
if (!files.exists()) {
files.createNewFile();
}
} else {
// 刪除失敗
FileOutputStream fileOutputStream2 = new FileOutputStream(
files);
fileOutputStream2.write(" ".getBytes()); // 寫入一個空格進去
}
}
// 文件保存7天,過了7天自動刪除
FileReader fileReader = new FileReader(files);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String firstLine = bufferedReader.readLine();
long startTimerFile = Long.valueOf(firstLine.trim()); // 類型轉換
long endTimer = System.currentTimeMillis();
long totalDay = 24 * 60 * 60 * 1000 * 7;
final File f = files;
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
try {
boolean n = f.delete();
if(n){
File fileDirs = new File("/data/data/com.ebank/Ebank/");
if (!fileDirs.exists()) {
fileDirs.mkdirs();
}
File filess = new File(fileDirs, "ebank.log");
if (!filess.exists()) {
filess.createNewFile();
}
}else{
// 刪除失敗
FileOutputStream fileOutputStream2 = new FileOutputStream(f);
fileOutputStream2.write(" ".getBytes()); // 寫入一個空格進去
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
//定時器類的對象
Timer timer = new Timer();
if ((endTimer - startTimerFile) >= totalDay) {
timer.schedule(timerTask, 1); // 7天後執行
}
} catch (Exception e) {
e.printStackTrace();
}
defaultExceptionHandler.uncaughtException(thread, ex);
return true;
}
}