diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index c960996b5..3a5b42efe 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -60,7 +60,8 @@ object AppConst { App.INSTANCE.putPrefBoolean("bookGroupAudio", value) } - const val notificationIdRead = 12 - const val notificationIdAudio = 13 - const val notificationIdWeb = 14 + const val notificationIdRead = 1144771 + const val notificationIdAudio = 1144772 + const val notificationIdWeb = 1144773 + const val notificationIdDownload = 1144774 } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index 5a1233708..2af217bc4 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -1,8 +1,49 @@ 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.constant.Action +import io.legado.app.constant.AppConst +import io.legado.app.help.IntentHelp 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(this, Action.stop) + ) + builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + val notification = builder.build() + startForeground(AppConst.notificationIdDownload, notification) + } } \ No newline at end of file