This commit is contained in:
kunfei 2023-02-15 23:33:30 +08:00
parent afe7af182c
commit 46a72d3265
7 changed files with 11 additions and 22 deletions

View File

@ -25,6 +25,7 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() {
override fun onCreate() {
super.onCreate()
LifecycleHelp.onServiceCreate(this)
upNotification()
checkNotificationPermission()
}

View File

@ -93,7 +93,6 @@ class AudioPlayService : BaseService(),
override fun onCreate() {
super.onCreate()
isRun = true
upNotification()
exoPlayer.addListener(this)
initMediaSession()
initBroadcastReceiver()

View File

@ -94,7 +94,6 @@ abstract class BaseReadAloudService : BaseService(),
observeLiveBus()
initMediaSession()
initBroadcastReceiver()
upNotification()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
setTimer(AppConfig.ttsTimer)
if (AppConfig.ttsTimer > 0) {

View File

@ -33,7 +33,7 @@ class CacheBookService : BaseService() {
private var cachePool =
Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
private var downloadJob: Job? = null
private var notificationContent = appCtx.getString(R.string.service_starting)
private val notificationBuilder by lazy {
val builder = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download)
@ -51,11 +51,11 @@ class CacheBookService : BaseService() {
override fun onCreate() {
super.onCreate()
isRun = true
upNotification(getString(R.string.starting_download))
launch {
while (isActive) {
delay(1000)
upNotification(CacheBook.downloadSummary)
notificationContent = CacheBook.downloadSummary
upNotification()
postEvent(EventBus.UP_DOWNLOAD, "")
}
}
@ -105,7 +105,8 @@ class CacheBookService : BaseService() {
end
}
cacheBook.addDownload(start, end2)
upNotification(CacheBook.downloadSummary)
notificationContent = CacheBook.downloadSummary
upNotification()
if (downloadJob == null) {
download()
}
@ -149,7 +150,7 @@ class CacheBookService : BaseService() {
/**
* 更新通知
*/
private fun upNotification(notificationContent: String) {
override fun upNotification() {
notificationBuilder.setContentText(notificationContent)
val notification = notificationBuilder.build()
startForeground(AppConst.notificationIdCache, notification)

View File

@ -31,6 +31,7 @@ import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import org.mozilla.javascript.WrappedException
import splitties.init.appCtx
import java.util.concurrent.Executors
import kotlin.math.min
@ -44,7 +45,7 @@ class CheckSourceService : BaseService() {
private val allIds = ArrayList<String>()
private val checkedIds = ArrayList<String>()
private var processIndex = 0
private var notificationMsg = ""
private var notificationMsg = appCtx.getString(R.string.service_starting)
private val notificationBuilder by lazy {
NotificationCompat.Builder(this, AppConst.channelIdReadAloud)
@ -62,12 +63,6 @@ class CheckSourceService : BaseService() {
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
}
override fun onCreate() {
super.onCreate()
notificationMsg = getString(R.string.start)
upNotification()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
IntentAction.start -> intent.getStringArrayListExtra("selectIds")?.let {

View File

@ -41,7 +41,6 @@ class DownloadService : BaseService() {
override fun onCreate() {
super.onCreate()
upSummaryNotification()
registerReceiver(downloadReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
}
@ -198,10 +197,7 @@ class DownloadService : BaseService() {
}
}
/**
* 更新下载进度条
*/
private fun upSummaryNotification() {
override fun upNotification() {
val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download)
.setSubText(getString(R.string.action_download))

View File

@ -49,7 +49,7 @@ class WebService : BaseService() {
}
private var httpServer: HttpServer? = null
private var webSocketServer: WebSocketServer? = null
private var notificationContent = ""
private var notificationContent = appCtx.getString(R.string.service_starting)
private val networkChangedListener by lazy {
NetworkChangedListener(this)
}
@ -58,8 +58,6 @@ class WebService : BaseService() {
super.onCreate()
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
isRun = true
notificationContent = getString(R.string.service_starting)
upNotification()
upTile(true)
networkChangedListener.register()
networkChangedListener.onNetworkChanged = {