@@ -5,7 +5,8 @@ package com.liubuyao.utils
5
5
*/
6
6
object CrashUtils {
7
7
8
- val DEFAULT = Thread .getDefaultUncaughtExceptionHandler()
8
+ private val DEFAULT = Thread .getDefaultUncaughtExceptionHandler()
9
+ private var isOpen = true
9
10
10
11
/* *
11
12
* 初始化
@@ -42,20 +43,36 @@ object CrashUtils {
42
43
onCrashListener : OnCrashListener ?
43
44
): Thread .UncaughtExceptionHandler {
44
45
return Thread .UncaughtExceptionHandler { thread, throwable ->
45
- val crashDateTime = MyUtils .getNowDateByCrash()
46
- val throwableInfoToString = MyUtils .getThrowableInfoToString(throwable)
47
- val phoneInfo = MyUtils .getPhoneInfo(thread)
48
- MyUtils .writeToFile(
49
- " ${path} crash_log_${crashDateTime} .txt" ,
50
- phoneInfo + throwableInfoToString,
51
- false
52
- )
46
+ val isGranted = MyUtils .isGrantedPermission(Constants .WRITE_EXTERNAL_STORAGE )
47
+ if (isGranted && isOpen) {
48
+ val crashDateTime = MyUtils .getNowDateByCrash()
49
+ val throwableInfoToString = MyUtils .getThrowableInfoToString(throwable)
50
+ val phoneInfo = MyUtils .getPhoneInfo(thread)
51
+ MyUtils .writeToFile(
52
+ " ${path} crash_log_${crashDateTime} .txt" ,
53
+ phoneInfo + throwableInfoToString,
54
+ false
55
+ )
56
+ } else {
57
+ if (isOpen) {
58
+ MyUtils .showToast(" 无存储权限,异常未写入文件!!" )
59
+ } else {
60
+ MyUtils .showToast(" 关闭写入文件!!" )
61
+ }
62
+ }
53
63
onCrashListener?.onCrash(thread, throwable)
54
64
// 需要用默认的接收器处理,否则会卡死一段时间
55
65
DEFAULT ?.uncaughtException(thread, throwable)
56
66
}
57
67
}
58
68
69
+ /* *
70
+ * 设置是否保存到文件
71
+ */
72
+ fun openCrashSaveFile (open : Boolean ) {
73
+ isOpen = open
74
+ }
75
+
59
76
interface OnCrashListener {
60
77
fun onCrash (thread : Thread , throwable : Throwable )
61
78
}
0 commit comments