diff --git a/app/src/main/java/io/legado/app/help/CrashHandler.kt b/app/src/main/java/io/legado/app/help/CrashHandler.kt index 2037ff2a8..c20390582 100644 --- a/app/src/main/java/io/legado/app/help/CrashHandler.kt +++ b/app/src/main/java/io/legado/app/help/CrashHandler.kt @@ -4,8 +4,10 @@ import android.annotation.SuppressLint import android.content.Context import android.net.Uri import android.os.Build +import android.os.Looper import android.webkit.WebSettings import io.legado.app.constant.AppConst +import io.legado.app.constant.AppLog import io.legado.app.exception.NoStackTraceException import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig @@ -37,9 +39,26 @@ class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler { * uncaughtException 回调函数 */ override fun uncaughtException(thread: Thread, ex: Throwable) { - ReadAloud.stop(context) - handleException(ex) - mDefaultHandler?.uncaughtException(thread, ex) + if (shouldAbsorb(ex)) { + AppLog.put("发生未捕获的异常\n${ex.localizedMessage}", ex) + Looper.loop() + } else { + ReadAloud.stop(context) + handleException(ex) + mDefaultHandler?.uncaughtException(thread, ex) + } + } + + private fun shouldAbsorb(e: Throwable): Boolean { + return when { + e::class.simpleName == "CannotDeliverBroadcastException" -> true + e is SecurityException && e.message?.contains( + "nor current process has android.permission.OBSERVE_GRANT_REVOKE_PERMISSIONS", + true + ) == true -> true + + else -> false + } } /** @@ -107,7 +126,8 @@ class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler { val time = format.format(Date()) val fileName = "crash-$time-$timestamp.log" try { - val backupPath = AppConfig.backupPath ?: throw NoStackTraceException("备份路径未配置") + val backupPath = AppConfig.backupPath + ?: throw NoStackTraceException("备份路径未配置") val uri = Uri.parse(backupPath) val fileDoc = FileDoc.fromUri(uri, true) fileDoc.createFileIfNotExist(fileName, "crash")