From d44d2a3b08b2f57287ae570b1e767baa70798f0e Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Mon, 8 Apr 2024 10:10:47 +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/service/AudioPlayService.kt | 24 +++++++++++------ .../app/service/BaseReadAloudService.kt | 26 ++++++++++--------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index b86aa55ab..b9f9c6185 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -584,7 +584,7 @@ class AudioPlayService : BaseService(), } } - private fun createNotification(): NotificationCompat.Builder { + private fun createNotification(): NotificationCompat.Builder { var nTitle: String = when { pause -> getString(R.string.audio_pause) timeMinute in 1..60 -> getString( @@ -642,11 +642,14 @@ class AudioPlayService : BaseService(), return builder } - private fun upAudioPlayNotification() { + private fun upAudioPlayNotification() { execute { - createNotification() - }.onSuccess { - notificationManager.notify(NotificationId.AudioPlayService, it.build()) + try { + val notification = createNotification() + notificationManager.notify(NotificationId.AudioPlayService, notification.build()) + } catch (e: Exception) { + AppLog.put("创建音频播放通知出错,${e.localizedMessage}", e, true) + } } } @@ -655,9 +658,14 @@ class AudioPlayService : BaseService(), */ override fun startForegroundNotification() { execute { - createNotification() - }.onSuccess { - startForeground(NotificationId.AudioPlayService, it.build()) + try { + val notification = createNotification() + startForeground(NotificationId.AudioPlayService, notification.build()) + } catch (e: Exception) { + AppLog.put("创建音频播放通知出错,${e.localizedMessage}", e, true) + //创建通知出错不结束服务就会崩溃,服务必须绑定通知 + stopSelf() + } } } diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 7cd36a8ff..ffd15c7b6 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -471,11 +471,12 @@ abstract class BaseReadAloudService : BaseService(), private fun upReadAloudNotification() { execute { - createNotification() - }.onSuccess { - notificationManager.notify(NotificationId.ReadAloudService, it.build()) - }.onError { - AppLog.put("创建朗读通知出错,${it.localizedMessage}", it, true) + try { + val notification = createNotification() + notificationManager.notify(NotificationId.ReadAloudService, notification.build()) + } catch (e: Exception) { + AppLog.put("创建朗读通知出错,${e.localizedMessage}", e, true) + } } } @@ -542,13 +543,14 @@ abstract class BaseReadAloudService : BaseService(), */ override fun startForegroundNotification() { execute { - createNotification() - }.onSuccess { - startForeground(NotificationId.ReadAloudService, it.build()) - }.onError { - AppLog.put("创建朗读通知出错,${it.localizedMessage}", it, true) - //创建通知出错不结束服务就会崩溃,服务必须绑定通知 - stopSelf() + try { + val notification = createNotification() + startForeground(NotificationId.ReadAloudService, notification.build()) + } catch (e: Exception) { + AppLog.put("创建朗读通知出错,${e.localizedMessage}", e, true) + //创建通知出错不结束服务就会崩溃,服务必须绑定通知 + stopSelf() + } } }