pull/51/head
parent
97d585690e
commit
f3fa9d3efe
@ -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<WebService>(this, Action.stop) |
||||
) |
||||
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
||||
val notification = builder.build() |
||||
startForeground(AppConst.notificationIdDownload, notification) |
||||
} |
||||
} |
Loading…
Reference in new issue