pull/2437/head
kunfei 2 years ago
parent fec8ae368b
commit c7e01ee373
  1. 1
      app/src/main/AndroidManifest.xml
  2. 8
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  3. 8
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  4. 9
      app/src/main/java/io/legado/app/service/WebService.kt

@ -4,6 +4,7 @@
package="io.legado.app"> package="io.legado.app">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

@ -9,6 +9,7 @@ import android.graphics.BitmapFactory
import android.media.AudioManager import android.media.AudioManager
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.PowerManager
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
@ -36,6 +37,7 @@ import io.legado.app.utils.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import splitties.systemservices.audioManager import splitties.systemservices.audioManager
import splitties.systemservices.powerManager
/** /**
* 音频播放服务 * 音频播放服务
@ -61,6 +63,9 @@ class AudioPlayService : BaseService(),
private set private set
} }
private val wakeLock by lazy {
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:webService")
}
private val mFocusRequest: AudioFocusRequestCompat by lazy { private val mFocusRequest: AudioFocusRequestCompat by lazy {
MediaHelp.buildAudioFocusRequestCompat(this) MediaHelp.buildAudioFocusRequestCompat(this)
} }
@ -82,8 +87,10 @@ class AudioPlayService : BaseService(),
private var upPlayProgressJob: Job? = null private var upPlayProgressJob: Job? = null
private var playSpeed: Float = 1f private var playSpeed: Float = 1f
@SuppressLint("WakelockTimeout")
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
wakeLock.acquire()
isRun = true isRun = true
upNotification() upNotification()
exoPlayer.addListener(this) exoPlayer.addListener(this)
@ -120,6 +127,7 @@ class AudioPlayService : BaseService(),
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
wakeLock.release()
isRun = false isRun = false
abandonFocus() abandonFocus()
exoPlayer.release() exoPlayer.release()

@ -8,6 +8,7 @@ import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.media.AudioManager import android.media.AudioManager
import android.os.PowerManager
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.CallSuper import androidx.annotation.CallSuper
@ -30,6 +31,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import splitties.systemservices.audioManager import splitties.systemservices.audioManager
import splitties.systemservices.powerManager
/** /**
* 朗读服务 * 朗读服务
@ -55,6 +57,9 @@ abstract class BaseReadAloudService : BaseService(),
} }
} }
private val wakeLock by lazy {
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:readAloud")
}
private val mFocusRequest: AudioFocusRequestCompat by lazy { private val mFocusRequest: AudioFocusRequestCompat by lazy {
MediaHelp.buildAudioFocusRequestCompat(this) MediaHelp.buildAudioFocusRequestCompat(this)
} }
@ -77,8 +82,10 @@ abstract class BaseReadAloudService : BaseService(),
} }
} }
@SuppressLint("WakelockTimeout")
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
wakeLock.acquire()
isRun = true isRun = true
pause = false pause = false
initMediaSession() initMediaSession()
@ -90,6 +97,7 @@ abstract class BaseReadAloudService : BaseService(),
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
wakeLock.release()
isRun = false isRun = false
pause = true pause = true
abandonFocus() abandonFocus()

@ -1,8 +1,10 @@
package io.legado.app.service package io.legado.app.service
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.PowerManager
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
@ -14,6 +16,7 @@ import io.legado.app.receiver.NetworkChangedListener
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.web.HttpServer import io.legado.app.web.HttpServer
import io.legado.app.web.WebSocketServer import io.legado.app.web.WebSocketServer
import splitties.systemservices.powerManager
import java.io.IOException import java.io.IOException
@ -33,6 +36,9 @@ class WebService : BaseService() {
} }
private val wakeLock by lazy {
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:webService")
}
private var httpServer: HttpServer? = null private var httpServer: HttpServer? = null
private var webSocketServer: WebSocketServer? = null private var webSocketServer: WebSocketServer? = null
private var notificationContent = "" private var notificationContent = ""
@ -40,8 +46,10 @@ class WebService : BaseService() {
NetworkChangedListener(this) NetworkChangedListener(this)
} }
@SuppressLint("WakelockTimeout")
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
wakeLock.acquire()
isRun = true isRun = true
notificationContent = getString(R.string.service_starting) notificationContent = getString(R.string.service_starting)
upNotification() upNotification()
@ -73,6 +81,7 @@ class WebService : BaseService() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
wakeLock.release()
networkChangedListener.unRegister() networkChangedListener.unRegister()
isRun = false isRun = false
if (httpServer?.isAlive == true) { if (httpServer?.isAlive == true) {

Loading…
Cancel
Save