From 51dd4d7064a634d0ea86e99895b3435bc8bd9aae Mon Sep 17 00:00:00 2001 From: nikk gitanes Date: Sat, 11 May 2024 03:49:08 +0300 Subject: [PATCH] avoid ANR with startForegroundService --- .../ru/yourok/torrserve/server/local/TorrService.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ru/yourok/torrserve/server/local/TorrService.kt b/app/src/main/java/ru/yourok/torrserve/server/local/TorrService.kt index 99ed82bd..f14aeba1 100644 --- a/app/src/main/java/ru/yourok/torrserve/server/local/TorrService.kt +++ b/app/src/main/java/ru/yourok/torrserve/server/local/TorrService.kt @@ -70,9 +70,9 @@ class TorrService : Service() { return START_NOT_STICKY } - private fun startServer(nfg: Boolean = false) { + private fun startServer(needForeground: Boolean = false) { serviceScope.launch { - if (BuildConfig.DEBUG) Log.d("TorrService", "startServer(nfg:$nfg)") + if (BuildConfig.DEBUG) Log.d("TorrService", "startServer(needForeground:$needForeground)") if (isLocal() && isAccessibilityOn() && !Accessibility.isEnabledService(App.context)) { if (BuildConfig.DEBUG) Log.d("TorrService", "Try to enable AssessibilityService") @@ -80,7 +80,7 @@ class TorrService : Service() { } if (serverFile.exists() && isLocal() && Api.echo().isEmpty()) { - if (nfg) { // fix start local server on boot + if (needForeground) { // fix start local server on boot val builder = NotificationCompat.Builder(this@TorrService, NotificationTS().channelId) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(getString(R.string.app_name)) @@ -131,7 +131,11 @@ class TorrService : Service() { if (isLocal()) { // avoid ANR on remote intent.putExtra(NEED_FOREGROUND_KEY, true) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent) + try { + context.startForegroundService(intent) + } catch (_: Exception) { + // need then call Service.startForeground() or ANR + } } } }