Horis vor 2 Jahren
Ursprung
Commit
fd2120e961

+ 9 - 9
app/src/main/java/io/legado/app/model/ReadAloud.kt

@@ -3,7 +3,6 @@ package io.legado.app.model
 import android.content.Context
 import android.content.Intent
 import android.os.Bundle
-import androidx.core.content.ContextCompat
 import io.legado.app.constant.AppLog
 import io.legado.app.constant.EventBus
 import io.legado.app.constant.IntentAction
@@ -16,6 +15,7 @@ import io.legado.app.service.TTSReadAloudService
 import io.legado.app.utils.LogUtils
 import io.legado.app.utils.StringUtils
 import io.legado.app.utils.postEvent
+import io.legado.app.utils.startForegroundServiceCompat
 import io.legado.app.utils.toastOnUi
 import splitties.init.appCtx
 
@@ -56,7 +56,7 @@ object ReadAloud {
         intent.putExtra("startPos", startPos)
         LogUtils.d("ReadAloud", intent.toString())
         try {
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         } catch (e: Exception) {
             val msg = "启动朗读服务出错\n${e.localizedMessage}"
             AppLog.put(msg, e)
@@ -81,7 +81,7 @@ object ReadAloud {
         if (BaseReadAloudService.isRun) {
             val intent = Intent(context, aloudClass)
             intent.action = IntentAction.pause
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         }
     }
 
@@ -89,7 +89,7 @@ object ReadAloud {
         if (BaseReadAloudService.isRun) {
             val intent = Intent(context, aloudClass)
             intent.action = IntentAction.resume
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         }
     }
 
@@ -97,7 +97,7 @@ object ReadAloud {
         if (BaseReadAloudService.isRun) {
             val intent = Intent(context, aloudClass)
             intent.action = IntentAction.stop
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         }
     }
 
@@ -105,7 +105,7 @@ object ReadAloud {
         if (BaseReadAloudService.isRun) {
             val intent = Intent(context, aloudClass)
             intent.action = IntentAction.prevParagraph
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         }
     }
 
@@ -113,7 +113,7 @@ object ReadAloud {
         if (BaseReadAloudService.isRun) {
             val intent = Intent(context, aloudClass)
             intent.action = IntentAction.nextParagraph
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         }
     }
 
@@ -121,7 +121,7 @@ object ReadAloud {
         if (BaseReadAloudService.isRun) {
             val intent = Intent(context, aloudClass)
             intent.action = IntentAction.upTtsSpeechRate
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         }
     }
 
@@ -130,7 +130,7 @@ object ReadAloud {
             val intent = Intent(context, aloudClass)
             intent.action = IntentAction.setTimer
             intent.putExtra("minute", minute)
-            ContextCompat.startForegroundService(context, intent)
+            context.startForegroundServiceCompat(intent)
         }
     }
 

+ 8 - 0
app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@@ -123,6 +123,14 @@ inline fun <reified T : BroadcastReceiver> Context.broadcastPendingIntent(
     return getBroadcast(this, 0, intent, flags)
 }
 
+fun Context.startForegroundServiceCompat(intent: Intent) {
+    try {
+        startService(intent)
+    } catch (e: IllegalStateException) {
+        ContextCompat.startForegroundService(this, intent)
+    }
+}
+
 val Context.defaultSharedPreferences: SharedPreferences
     get() = PreferenceManager.getDefaultSharedPreferences(this)