Bläddra i källkod

集成新的CrashHandler

Frankensteinly 8 månader sedan
förälder
incheckning
e7c17c1de4

+ 3 - 0
app/src/main/java/com/grkj/iscs/MyApplication.kt

@@ -9,6 +9,7 @@ import com.grkj.iscs.util.CrashUtil
 import com.grkj.iscs.util.Executor
 import com.grkj.iscs.util.FileUtil
 import com.grkj.iscs.util.FileUtil.LOG_DIR
+import com.grkj.iscs.util.MyCrashHandler
 import com.grkj.iscs.util.NetApi
 import com.grkj.iscs.util.NetHttpManager
 import com.grkj.iscs.util.SPUtils
@@ -40,6 +41,8 @@ class MyApplication : Application() {
             BusinessManager.registerMainListener()
         }
         LogUtil.i("App start")
+
+        Thread.setDefaultUncaughtExceptionHandler(MyCrashHandler())
     }
 
     override fun attachBaseContext(base: Context?) {

+ 0 - 110
app/src/main/java/com/grkj/iscs/util/MyCrashHandler.java

@@ -1,110 +0,0 @@
-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();
-    }
-
-}

+ 103 - 0
app/src/main/java/com/grkj/iscs/util/MyCrashHandler.kt

@@ -0,0 +1,103 @@
+package com.grkj.iscs.util
+
+import android.content.Intent
+import android.os.Process
+import android.text.TextUtils
+import com.grkj.iscs.MyApplication.Companion.instance
+import com.grkj.iscs.util.FileUtil.CRASH_DIR
+import com.grkj.iscs.util.FileUtil.getRootFolder
+import com.grkj.iscs.util.log.LogUtil
+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
+import kotlin.system.exitProcess
+
+class MyCrashHandler : Thread.UncaughtExceptionHandler {
+    override fun uncaughtException(t: Thread, e: Throwable) {
+        LogUtil.e("程序出现异常了, Thread = ${t.name}, Throwable = ${e.message}")
+        val stackTraceInfo = getStackTraceInfo(e)
+        LogUtil.e("stackTraceInfo : $stackTraceInfo")
+        saveThrowableMessage(stackTraceInfo)
+        
+        val ctx = instance!!.applicationContext
+        val intent = ctx.packageManager.getLaunchIntentForPackage(ctx.packageName)
+        intent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
+        ctx.startActivity(intent)
+        Process.killProcess(Process.myPid())
+        exitProcess(0)
+    }
+
+    /**
+     * 获取错误的信息
+     *
+     * @param throwable
+     * @return
+     */
+    private fun getStackTraceInfo(throwable: Throwable): String {
+        var pw: PrintWriter? = null
+        val writer: Writer = StringWriter()
+        try {
+            pw = PrintWriter(writer)
+            throwable.printStackTrace(pw)
+        } catch (e: Exception) {
+            return ""
+        } finally {
+            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 val logFilePath = getRootFolder(instance!!.applicationContext, 0)!!.absolutePath + CRASH_DIR
+
+    private fun saveThrowableMessage(errorMessage: String) {
+        if (TextUtils.isEmpty(errorMessage)) {
+            return
+        }
+        val file = File(logFilePath)
+        if (!file.exists()) {
+            val mkdirs = file.mkdirs()
+            if (mkdirs) {
+                writeStringToFile(errorMessage, file)
+            }
+        } else {
+            writeStringToFile(errorMessage, file)
+        }
+    }
+
+    private fun writeStringToFile(errorMessage: String, file: File) {
+        Thread {
+            var outputStream: FileOutputStream? = null
+            try {
+                val inputStream = ByteArrayInputStream(errorMessage.toByteArray())
+                outputStream =
+                    FileOutputStream(File(file, System.currentTimeMillis().toString() + ".txt"))
+                var len = 0
+                val bytes = ByteArray(1024)
+                while ((inputStream.read(bytes).also { len = it }) != -1) {
+                    outputStream.write(bytes, 0, len)
+                }
+                outputStream.flush()
+                LogUtil.e("程序出异常了, 写入本地文件成功:${file.absolutePath}")
+            } catch (e: FileNotFoundException) {
+                e.printStackTrace()
+            } catch (e: IOException) {
+                e.printStackTrace()
+            } finally {
+                if (outputStream != null) {
+                    try {
+                        outputStream.close()
+                    } catch (e: IOException) {
+                        e.printStackTrace()
+                    }
+                }
+            }
+        }.start()
+    }
+}

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -206,6 +206,7 @@
     <string name="please_take_out_ready_device_first">请先取出已开卡扣的设备</string>
     <string name="can_not_do_colock_action">当前无法进行共锁相关操作</string>
     <string name="key_is_in_failure_mode">钥匙处于故障模式</string>
+    <string name="uncaught_exception_tip">很抱歉,程序出现异常,即将退出</string>
 
     <!-- 物资柜 -->
     <string name="material_management_system">物资管理系统</string>