|
|
@@ -0,0 +1,110 @@
|
|
|
+package com.grkj.iscs.util;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Environment;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.grkj.iscs.MyApplication;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.io.StringWriter;
|
|
|
+import java.io.Writer;
|
|
|
+
|
|
|
+public class MyCrashHandler implements Thread.UncaughtExceptionHandler {
|
|
|
+ @Override
|
|
|
+ public void uncaughtException(Thread t, Throwable e) {
|
|
|
+ Log.e("程序出现异常了", "Thread = " + t.getName() + "\nThrowable = " + e.getMessage());
|
|
|
+ String stackTraceInfo = getStackTraceInfo(e);
|
|
|
+ Log.e("stackTraceInfo", stackTraceInfo);
|
|
|
+ saveThrowableMessage(stackTraceInfo);
|
|
|
+
|
|
|
+ Context ctx = MyApplication.Companion.getInstance().getApplicationContext();
|
|
|
+ Intent intent = ctx.getPackageManager().getLaunchIntentForPackage(ctx.getPackageName());
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
+ ctx.startActivity(intent);
|
|
|
+ android.os.Process.killProcess(android.os.Process.myPid());
|
|
|
+ System.exit(0);
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取错误的信息
|
|
|
+ *
|
|
|
+ * @param throwable
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getStackTraceInfo(final Throwable throwable) {
|
|
|
+ PrintWriter pw = null;
|
|
|
+ Writer writer = new StringWriter();
|
|
|
+ try {
|
|
|
+ pw = new PrintWriter(writer);
|
|
|
+ throwable.printStackTrace(pw);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "";
|
|
|
+ } finally {
|
|
|
+ if (pw != null) {
|
|
|
+ pw.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return writer.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+// private String logFilePath = Environment.getExternalStorageDirectory() + File.separator + "Android" +
|
|
|
+// File.separator + "data" + File.separator + MyApplication.Companion.getInstance().getPackageName() + File.separator + "crashLog";
|
|
|
+
|
|
|
+ private String logFilePath = FileUtil.INSTANCE.getRootFolder(MyApplication.Companion.getInstance().getApplicationContext(), 0).getAbsolutePath() + FileUtil.INSTANCE.getCRASH_DIR();
|
|
|
+
|
|
|
+ private void saveThrowableMessage(String errorMessage) {
|
|
|
+ if (TextUtils.isEmpty(errorMessage)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File file = new File(logFilePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ boolean mkdirs = file.mkdirs();
|
|
|
+ if (mkdirs) {
|
|
|
+ writeStringToFile(errorMessage, file);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ writeStringToFile(errorMessage, file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void writeStringToFile(final String errorMessage, final File file) {
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ FileOutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(errorMessage.getBytes());
|
|
|
+ outputStream = new FileOutputStream(new File(file, System.currentTimeMillis() + ".txt"));
|
|
|
+ int len = 0;
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ while ((len = inputStream.read(bytes)) != -1) {
|
|
|
+ outputStream.write(bytes, 0, len);
|
|
|
+ }
|
|
|
+ outputStream.flush();
|
|
|
+ Log.e("程序出异常了", "写入本地文件成功:" + file.getAbsolutePath());
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|