diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 825360885..5a4dde7bd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ package="io.legado.app"> + 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 8949774e9..a5354cabf 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -9,6 +9,7 @@ import android.graphics.BitmapFactory import android.media.AudioManager import android.net.Uri import android.os.Build +import android.os.PowerManager import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import androidx.core.app.NotificationCompat @@ -36,6 +37,7 @@ import io.legado.app.utils.* import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.Main import splitties.systemservices.audioManager +import splitties.systemservices.powerManager /** * 音频播放服务 @@ -61,6 +63,9 @@ class AudioPlayService : BaseService(), private set } + private val wakeLock by lazy { + powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:webService") + } private val mFocusRequest: AudioFocusRequestCompat by lazy { MediaHelp.buildAudioFocusRequestCompat(this) } @@ -82,8 +87,10 @@ class AudioPlayService : BaseService(), private var upPlayProgressJob: Job? = null private var playSpeed: Float = 1f + @SuppressLint("WakelockTimeout") override fun onCreate() { super.onCreate() + wakeLock.acquire() isRun = true upNotification() exoPlayer.addListener(this) @@ -120,6 +127,7 @@ class AudioPlayService : BaseService(), override fun onDestroy() { super.onDestroy() + wakeLock.release() isRun = false abandonFocus() exoPlayer.release() 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 2a185fa1d..7547ad987 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -8,6 +8,7 @@ import android.content.Intent import android.content.IntentFilter import android.graphics.BitmapFactory import android.media.AudioManager +import android.os.PowerManager import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import androidx.annotation.CallSuper @@ -30,6 +31,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch 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 { MediaHelp.buildAudioFocusRequestCompat(this) } @@ -77,8 +82,10 @@ abstract class BaseReadAloudService : BaseService(), } } + @SuppressLint("WakelockTimeout") override fun onCreate() { super.onCreate() + wakeLock.acquire() isRun = true pause = false initMediaSession() @@ -90,6 +97,7 @@ abstract class BaseReadAloudService : BaseService(), override fun onDestroy() { super.onDestroy() + wakeLock.release() isRun = false pause = true abandonFocus() diff --git a/app/src/main/java/io/legado/app/service/WebService.kt b/app/src/main/java/io/legado/app/service/WebService.kt index c18c95f82..1f408e143 100644 --- a/app/src/main/java/io/legado/app/service/WebService.kt +++ b/app/src/main/java/io/legado/app/service/WebService.kt @@ -1,8 +1,10 @@ package io.legado.app.service +import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.os.Build +import android.os.PowerManager import androidx.core.app.NotificationCompat import io.legado.app.R 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.web.HttpServer import io.legado.app.web.WebSocketServer +import splitties.systemservices.powerManager 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 webSocketServer: WebSocketServer? = null private var notificationContent = "" @@ -40,8 +46,10 @@ class WebService : BaseService() { NetworkChangedListener(this) } + @SuppressLint("WakelockTimeout") override fun onCreate() { super.onCreate() + wakeLock.acquire() isRun = true notificationContent = getString(R.string.service_starting) upNotification() @@ -73,6 +81,7 @@ class WebService : BaseService() { override fun onDestroy() { super.onDestroy() + wakeLock.release() networkChangedListener.unRegister() isRun = false if (httpServer?.isAlive == true) {