|
|
|
@ -20,18 +20,14 @@ import java.util.concurrent.TimeUnit |
|
|
|
|
* 异常管理类 |
|
|
|
|
*/ |
|
|
|
|
@Suppress("DEPRECATION") |
|
|
|
|
class CrashHandler : Thread.UncaughtExceptionHandler { |
|
|
|
|
class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler { |
|
|
|
|
private val tag = this.javaClass.simpleName |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 系统默认UncaughtExceptionHandler |
|
|
|
|
*/ |
|
|
|
|
private var mDefaultHandler: Thread.UncaughtExceptionHandler? = null |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* context |
|
|
|
|
*/ |
|
|
|
|
private var mContext: Context? = null |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 存储异常和参数信息 |
|
|
|
|
*/ |
|
|
|
@ -43,8 +39,7 @@ class CrashHandler : Thread.UncaughtExceptionHandler { |
|
|
|
|
@SuppressLint("SimpleDateFormat") |
|
|
|
|
private val format = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss") |
|
|
|
|
|
|
|
|
|
fun init(context: Context) { |
|
|
|
|
mContext = context |
|
|
|
|
init { |
|
|
|
|
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler() |
|
|
|
|
//设置该CrashHandler为系统默认的 |
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(this) |
|
|
|
@ -65,14 +60,14 @@ class CrashHandler : Thread.UncaughtExceptionHandler { |
|
|
|
|
private fun handleException(ex: Throwable?) { |
|
|
|
|
if (ex == null) return |
|
|
|
|
//收集设备参数信息 |
|
|
|
|
collectDeviceInfo(mContext!!) |
|
|
|
|
collectDeviceInfo(context) |
|
|
|
|
//添加自定义信息 |
|
|
|
|
addCustomInfo() |
|
|
|
|
kotlin.runCatching { |
|
|
|
|
//使用Toast来显示异常信息 |
|
|
|
|
Handler(Looper.getMainLooper()).post { |
|
|
|
|
Toast.makeText( |
|
|
|
|
mContext, |
|
|
|
|
context, |
|
|
|
|
ex.message, |
|
|
|
|
Toast.LENGTH_LONG |
|
|
|
|
).show() |
|
|
|
@ -141,7 +136,7 @@ class CrashHandler : Thread.UncaughtExceptionHandler { |
|
|
|
|
val timestamp = System.currentTimeMillis() |
|
|
|
|
val time = format.format(Date()) |
|
|
|
|
val fileName = "crash-$time-$timestamp.log" |
|
|
|
|
mContext?.externalCacheDir?.let { rootFile -> |
|
|
|
|
context.externalCacheDir?.let { rootFile -> |
|
|
|
|
FileUtils.getFile(rootFile, "crash").listFiles()?.forEach { |
|
|
|
|
if (it.lastModified() < System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7)) { |
|
|
|
|
it.delete() |
|
|
|
|