优化
This commit is contained in:
@@ -45,19 +45,18 @@ class CrashHandler private constructor(private val context: Context) :
|
||||
}
|
||||
}
|
||||
|
||||
fun getCrashReportFiles(context: Context): Array<File> {
|
||||
// val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
||||
val crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
||||
return if (crashDir.exists() && crashDir.isDirectory) {
|
||||
crashDir.listFiles { _, name -> name.endsWith(".log") } ?: emptyArray()
|
||||
} else {
|
||||
emptyArray()
|
||||
}
|
||||
}
|
||||
|
||||
fun clearCrashReports(context: Context) {
|
||||
getCrashReportFiles(context).forEach { it.delete() }
|
||||
}
|
||||
//fun getCrashReportFiles(context: Context): Array<File> {
|
||||
// val crashDir = getCrashDir()
|
||||
// return if (crashDir.exists() && crashDir.isDirectory) {
|
||||
// crashDir.listFiles { _, name -> name.endsWith(".log") } ?: emptyArray()
|
||||
// } else {
|
||||
// emptyArray()
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//fun clearCrashReports(context: Context) {
|
||||
// getCrashReportFiles(context).forEach { it.delete() }
|
||||
//}
|
||||
}
|
||||
|
||||
private val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||
@@ -69,6 +68,11 @@ class CrashHandler private constructor(private val context: Context) :
|
||||
override fun uncaughtException(thread: Thread, ex: Throwable) {
|
||||
handleException(thread, ex)
|
||||
|
||||
try {
|
||||
Thread.sleep(3000)
|
||||
} catch (e: InterruptedException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
// 如果系统提供了默认的异常处理器,则交给系统去结束程序
|
||||
// 否则自己结束程序
|
||||
defaultHandler?.uncaughtException(thread, ex) ?: run {
|
||||
@@ -204,11 +208,8 @@ class CrashHandler private constructor(private val context: Context) :
|
||||
val time = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.getDefault()).format(Date())
|
||||
val fileName = "crash_$time.log"
|
||||
|
||||
// val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
||||
val crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
||||
if (!crashDir.exists()) {
|
||||
crashDir.mkdirs()
|
||||
}
|
||||
val crashDir = getCrashDir()
|
||||
|
||||
val crashFile = File(crashDir, fileName)
|
||||
FileOutputStream(crashFile).use { it.write(crashInfo.toByteArray()) }
|
||||
|
||||
@@ -222,7 +223,7 @@ class CrashHandler private constructor(private val context: Context) :
|
||||
* 清理旧的崩溃日志
|
||||
*/
|
||||
fun cleanupOldCrashReports(maxAgeDays: Int = 7) {
|
||||
val crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
||||
val crashDir = getCrashDir()
|
||||
if (!crashDir.exists() || !crashDir.isDirectory) return
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
@@ -234,4 +235,18 @@ class CrashHandler private constructor(private val context: Context) :
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCrashDir():File {
|
||||
//val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR)
|
||||
var crashDir = File(context.filesDir, CRASH_REPORTS_DIR)
|
||||
if (!crashDir.exists()) {
|
||||
crashDir.mkdirs()
|
||||
}
|
||||
|
||||
if (!(crashDir.exists())) {
|
||||
crashDir = File(context.cacheDir, CRASH_REPORTS_DIR)
|
||||
}
|
||||
return crashDir
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user