pull/51/head
kunfei 5 years ago
parent 97d585690e
commit f3fa9d3efe
  1. 7
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 41
      app/src/main/java/io/legado/app/service/DownloadService.kt

@ -60,7 +60,8 @@ object AppConst {
App.INSTANCE.putPrefBoolean("bookGroupAudio", value) App.INSTANCE.putPrefBoolean("bookGroupAudio", value)
} }
const val notificationIdRead = 12 const val notificationIdRead = 1144771
const val notificationIdAudio = 13 const val notificationIdAudio = 1144772
const val notificationIdWeb = 14 const val notificationIdWeb = 1144773
const val notificationIdDownload = 1144774
} }

@ -1,8 +1,49 @@
package io.legado.app.service package io.legado.app.service
import android.content.Intent
import androidx.core.app.NotificationCompat
import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.Action
import io.legado.app.constant.AppConst
import io.legado.app.help.IntentHelp
class DownloadService : BaseService() { class DownloadService : BaseService() {
override fun onCreate() {
super.onCreate()
updateNotification("正在启动下载")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
intent?.action?.let { action ->
when (action) {
Action.stop -> stopSelf()
}
}
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
}
/**
* 更新通知
*/
private fun updateNotification(content: String) {
val builder = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download)
.setOngoing(true)
.setContentTitle(getString(R.string.download_offline))
.setContentText(content)
builder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
IntentHelp.servicePendingIntent<WebService>(this, Action.stop)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
val notification = builder.build()
startForeground(AppConst.notificationIdDownload, notification)
}
} }
Loading…
Cancel
Save