From dba756c2f40150f3565f58be99e5f790372ba8c7 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Thu, 15 Feb 2024 16:48:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E9=80=9A=E7=9F=A5=E6=A0=8F=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=B8=A4=E6=9D=A1=E5=B8=B8=E9=A9=BB=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=20#392?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forwarder/service/ForegroundService.kt | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/service/ForegroundService.kt b/app/src/main/java/com/idormy/sms/forwarder/service/ForegroundService.kt index 2341c360..4ed5439b 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/service/ForegroundService.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/service/ForegroundService.kt @@ -33,7 +33,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async @SuppressLint("SimpleDateFormat") -@Suppress("PrivatePropertyName", "DeferredResultUnused", "OPT_IN_USAGE", "DEPRECATION", "LiftReturnOrAssignment") +@Suppress("PrivatePropertyName", "DeferredResultUnused", "OPT_IN_USAGE", "DEPRECATION") class ForegroundService : Service() { private val TAG: String = ForegroundService::class.java.simpleName @@ -85,7 +85,7 @@ class ForegroundService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { //纯客户端模式 - if (SettingUtils.enablePureClientMode) return START_STICKY + if (SettingUtils.enablePureClientMode) return START_NOT_STICKY if (intent != null) { when (intent.action) { @@ -118,6 +118,9 @@ class ForegroundService : Service() { } private fun startForegroundService() { + if (isRunning) return + isRunning = true + val notification = createNotification(SettingUtils.notifyContent) startForeground(NOTIFICATION_ID, notification) @@ -164,12 +167,8 @@ class ForegroundService : Service() { } } } - - isRunning = true } catch (e: Exception) { - e.printStackTrace() - Log.e(TAG, "startForegroundService: $e") - isRunning = false + handleException(e, "startForegroundService") } } @@ -181,9 +180,7 @@ class ForegroundService : Service() { compositeDisposable.dispose() isRunning = false } catch (e: Exception) { - e.printStackTrace() - Log.e(TAG, "stopForegroundService: $e") - isRunning = true + handleException(e, "stopForegroundService") } } @@ -212,8 +209,18 @@ class ForegroundService : Service() { } private fun updateNotification(updatedContent: String) { - val notification = createNotification(updatedContent) - notificationManager?.notify(NOTIFICATION_ID, notification) + try { + val notification = createNotification(updatedContent) + notificationManager?.notify(NOTIFICATION_ID, notification) + } catch (e: Exception) { + handleException(e, "updateNotification") + } + } + + private fun handleException(e: Exception, methodName: String) { + e.printStackTrace() + Log.e(TAG, "$methodName: $e") + isRunning = false } } \ No newline at end of file