我正在实现IntentService下载PDF文件,它会向用户显示通知downloadStart并在此过程中不断更新进度,服务运行正常并且进度更新正确,问题是我从“最近”下载停止,甚至没有显示错误。这是我的DownloadService类:class DownloadService : IntentService("DownloadService") {lateinit var downloadNotification : DownloadNotificationlateinit var book : BookDataprivate lateinit var fileName : Stringprivate lateinit var fileFolder : Stringprivate lateinit var filePath : Stringlateinit var fileUrl : Stringvar isCancelled = falseprivate lateinit var handler : Handleroverride fun onCreate() { super.onCreate() handler = Handler()}override fun onHandleIntent(p0: Intent?) { book = Gson().fromJson<BookData>(p0?.getStringExtra("book"), BookData::class.java) downloadNotification = DownloadNotification(this, book.id!!) init(book)}fun getFilePath() : String { val directory = File(fileFolder) if (!directory.exists()) { directory.mkdirs() } return filePath}private fun init(book : BookData) { fileName = "${book.id}.pdf" fileFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString() + File.separator + "Libranova/Books/" filePath = fileFolder + fileName fileUrl = book.downloadLink!! startDownload()}private fun startDownload() { downloadNotification.setTitle(book.name!!).setText("Preparing...").notificationCompat.apply { downloadNotification.notifyManager(true) DownloadUtils.downloadFile(this@DownloadService, object : DownloadListener { override fun onStarted() { handler.post { Toast.makeText(this@DownloadService,"Download Started", Toast.LENGTH_LONG).show() } } override fun onSuccess() { downloadNotification.onFinishDownload().freeUp().setSuccess().notifyManager(true) } override fun onError(message: String) { downloadNotification.onFinishDownload().freeUp().setError(message).notifyManager(true) }
添加回答
举报
0/150
提交
取消