|
|
@@ -1,5 +1,10 @@
|
|
|
package io.legado.app.help
|
|
|
|
|
|
+import android.annotation.SuppressLint
|
|
|
+import android.content.BroadcastReceiver
|
|
|
+import android.content.Context
|
|
|
+import android.content.Intent
|
|
|
+import android.content.IntentFilter
|
|
|
import android.os.Handler
|
|
|
import android.os.HandlerThread
|
|
|
import android.os.SystemClock
|
|
|
@@ -14,11 +19,19 @@ object AppFreezeMonitor {
|
|
|
Handler(HandlerThread("AppFreezeMonitor").apply { start() }.looper)
|
|
|
}
|
|
|
|
|
|
- fun init() {
|
|
|
+ val screenStatusReceiver by lazy {
|
|
|
+ ScreenStatusReceiver()
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressLint("UnspecifiedRegisterReceiverFlag")
|
|
|
+ fun init(context: Context) {
|
|
|
if (!AppConfig.recordLog) {
|
|
|
+ context.unregisterReceiver(screenStatusReceiver)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ context.registerReceiver(screenStatusReceiver, screenStatusReceiver.filter)
|
|
|
+
|
|
|
var previous = SystemClock.uptimeMillis()
|
|
|
|
|
|
val runnable = object : Runnable {
|
|
|
@@ -27,7 +40,7 @@ object AppFreezeMonitor {
|
|
|
val elapsed = current - previous
|
|
|
val extra = elapsed - 3000
|
|
|
|
|
|
- if (extra > 100) {
|
|
|
+ if (extra > 300) {
|
|
|
LogUtils.d(TAG, "检测到应用被系统冻结,时长:$extra 毫秒")
|
|
|
}
|
|
|
|
|
|
@@ -41,4 +54,19 @@ object AppFreezeMonitor {
|
|
|
handler.postDelayed(runnable, 3000)
|
|
|
}
|
|
|
|
|
|
+ class ScreenStatusReceiver : BroadcastReceiver() {
|
|
|
+
|
|
|
+ val filter = IntentFilter().apply {
|
|
|
+ addAction(Intent.ACTION_SCREEN_ON)
|
|
|
+ addAction(Intent.ACTION_SCREEN_OFF)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onReceive(context: Context?, intent: Intent?) {
|
|
|
+ when (intent?.action) {
|
|
|
+ Intent.ACTION_SCREEN_ON -> LogUtils.d(TAG, "SCREEN_ON")
|
|
|
+ Intent.ACTION_SCREEN_OFF -> LogUtils.d(TAG, "SCREEN_OFF")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|