1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package com.dolin.zap.crashlytics
- import android.os.Process
- import com.dolin.zap.Zap
- import kotlin.system.exitProcess
- /**
- * @author #Suyghur.
- * Created on 4/12/21
- */
- class JavaCrashCatcher : Thread.UncaughtExceptionHandler {
- private val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
- private var intercept = false
- fun initialize() {
- try {
- Thread.setDefaultUncaughtExceptionHandler(this)
- } catch (e: Exception) {
- Zap.e("JavaCrashCatcher->setDefaultUncaughtExceptionHandler() error , ${e.localizedMessage}")
- }
- }
- override fun uncaughtException(t: Thread, e: Throwable) {
- if (defaultHandler != null) {
- Thread.setDefaultUncaughtExceptionHandler(defaultHandler)
- }
- try {
- handleException(t, e)
- } catch (e: Exception) {
- Zap.e("JavaCrashCatcher->handlerException error , ${e.localizedMessage}")
- }
- if (intercept) {
- Process.killProcess(Process.myPid())
- exitProcess(10)
- } else {
- defaultHandler?.apply {
- uncaughtException(t, e)
- }
- }
- }
- private fun handleException(t: Thread, e: Throwable) {
- }
- companion object {
- fun getInstance(): JavaCrashCatcher {
- return JavaCrashCatcherHolder.INSTANCE
- }
- private object JavaCrashCatcherHolder {
- val INSTANCE = JavaCrashCatcher()
- }
- /**
- * 防止单例对象在反序列化时重新生成对象
- */
- private fun readResolve(): Any {
- return JavaCrashCatcherHolder.INSTANCE
- }
- }
- }
|