|
@@ -5,6 +5,7 @@ import android.app.AlarmManager;
|
|
|
import android.app.PendingIntent;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.os.Environment;
|
|
|
import android.os.Looper;
|
|
|
import android.util.Log;
|
|
|
|
|
@@ -15,6 +16,16 @@ import com.wdkl.callingdoor.common.Constants;
|
|
|
import com.wdkl.callingdoor.util.AutoRebootUtil;
|
|
|
import com.wdkl.callingdoor.util.SharedPreferencesUtil;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.io.StringWriter;
|
|
|
+import java.io.Writer;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
import static com.wdkl.callingdoor.common.Constants.NET_ERROR_FIVE_AFTER_TOAST;
|
|
|
|
|
|
/**
|
|
@@ -31,6 +42,15 @@ public class AnrFcExceptionUtil implements Thread.UncaughtExceptionHandler {
|
|
|
|
|
|
private static AnrFcExceptionUtil mAnrFcExceptionUtil;
|
|
|
|
|
|
+ /**
|
|
|
+ * 存储异常和参数信息
|
|
|
+ */
|
|
|
+ private Map<String, String> paramsMap = new HashMap<>();
|
|
|
+ /**
|
|
|
+ * 格式化时间
|
|
|
+ */
|
|
|
+ private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
|
|
+
|
|
|
public static AnrFcExceptionUtil getInstance(MyApplication application) {
|
|
|
if (mAnrFcExceptionUtil == null) {
|
|
|
mAnrFcExceptionUtil = new AnrFcExceptionUtil(application);
|
|
@@ -80,7 +100,7 @@ public class AnrFcExceptionUtil implements Thread.UncaughtExceptionHandler {
|
|
|
return false;
|
|
|
}
|
|
|
//使用Toast来显示异常信息
|
|
|
- new Thread() {
|
|
|
+ /*new Thread() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
Looper.prepare();
|
|
@@ -88,10 +108,58 @@ public class AnrFcExceptionUtil implements Thread.UncaughtExceptionHandler {
|
|
|
// Toast.LENGTH_SHORT).show();
|
|
|
Looper.loop();
|
|
|
}
|
|
|
- }.start();
|
|
|
+ }.start();*/
|
|
|
+ saveCrashInfo2File(ex);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 保存错误信息到文件中
|
|
|
+ *
|
|
|
+ * @param ex
|
|
|
+ * @return 返回文件名称
|
|
|
+ */
|
|
|
+ private String saveCrashInfo2File(Throwable ex) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+ sb.append(key + "=" + value + "\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ Writer writer = new StringWriter();
|
|
|
+ PrintWriter printWriter = new PrintWriter(writer);
|
|
|
+ ex.printStackTrace(printWriter);
|
|
|
+ Throwable cause = ex.getCause();
|
|
|
+ while (cause != null) {
|
|
|
+ cause.printStackTrace(printWriter);
|
|
|
+ cause = cause.getCause();
|
|
|
+ }
|
|
|
+ printWriter.close();
|
|
|
+ String result = writer.toString();
|
|
|
+ sb.append(result);
|
|
|
+ try {
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ String time = format.format(new Date());
|
|
|
+ String fileName = "crash-" + time + "-" + timestamp + ".log";
|
|
|
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
|
+ String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/crash/";
|
|
|
+ File dir = new File(path);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream fos = new FileOutputStream(path + fileName);
|
|
|
+ fos.write(sb.toString().getBytes());
|
|
|
+ Log.i(TAG, "saveCrashInfo2File: "+sb.toString());
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ return fileName;
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.e(TAG, "an error occured while writing file...", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* ===================================================崩溃异常处理===================================================
|