From e74024d4c827258eca5edd4b909dd24ae714f044 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:03:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/AppFreezeMonitor.kt | 32 +++++++++++++++++-- .../app/ui/config/OtherConfigFragment.kt | 2 +- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/AppFreezeMonitor.kt b/app/src/main/java/io/legado/app/help/AppFreezeMonitor.kt index 4a3f2960b..f8f9070d7 100644 --- a/app/src/main/java/io/legado/app/help/AppFreezeMonitor.kt +++ b/app/src/main/java/io/legado/app/help/AppFreezeMonitor.kt @@ -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") + } + } + } + } diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index ddeae47be..187f6465c 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -177,7 +177,7 @@ class OtherConfigFragment : PreferenceFragment(), LogUtils.upLevel() LogUtils.logDeviceInfo() LiveEventBus.config().enableLogger(AppConfig.recordLog) - AppFreezeMonitor.init() + AppFreezeMonitor.init(appCtx) } PreferKey.processText -> sharedPreferences?.let {