Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java IntentService/Service-即使应用程序终止也保持运行-Android_Java_Android_Kotlin_Service_Android Service - Fatal编程技术网

Java IntentService/Service-即使应用程序终止也保持运行-Android

Java IntentService/Service-即使应用程序终止也保持运行-Android,java,android,kotlin,service,android-service,Java,Android,Kotlin,Service,Android Service,我正在实现一个IntentService来下载一个PDF文件,它会在下载时向用户显示一个通知开始并在此过程中不断更新进度,该服务工作正常,进度更新正确,问题是只要我从最近的下载站点删除我的应用程序,甚至不会显示错误 以下是我的下载服务课程: 类下载服务:IntentServiceDownloadService{ lateinit var downloadNotification:downloadNotification lateinit变量书:BookData 私有lateinit变量文件名:

我正在实现一个IntentService来下载一个PDF文件,它会在下载时向用户显示一个通知开始并在此过程中不断更新进度,该服务工作正常,进度更新正确,问题是只要我从最近的下载站点删除我的应用程序,甚至不会显示错误

以下是我的下载服务课程: 类下载服务:IntentServiceDownloadService{ lateinit var downloadNotification:downloadNotification lateinit变量书:BookData 私有lateinit变量文件名:String 私有lateinit var文件文件夹:字符串 私有lateinit var文件路径:字符串 lateinit var fileUrl:字符串 var isCancelled=false 私有lateinit变量处理程序:处理程序 重写一次创建{ super.onCreate 处理程序=处理程序 } 覆盖有趣的onHandleIntentp0:意图{ book=Gson.fromJsonp0?.getStringExtrabook,BookData::class.java downloadNotification=DownloadNotificationthis,book.id!! initbook } fun getFilePath:String{ val directory=filefolder 如果!目录.1存在{ directory.mkdirs } 返回文件路径 } 私人书籍:BookData{ fileName=${book.id}.pdf fileFolder=Environment.getExternalStoragePublicDirectoryEnvironment.DIRECTORY\u DOCUMENTS.toString+File.separator+Libranova/Books/ filePath=fileFolder+fileName fileUrl=book.downloadLink!! 开始下降负荷 } 私人娱乐开始下载{ downloadNotification.setTitlebook.name!!.setTextPreparing….notificationCompat.apply{ downloadNotification.notifyManagertrue 下载Utils。downloadFilethis@DownloadService,对象:DownloadListener{ 游戏开始了{ handler.post{ 干杯makeTextthis@DownloadService,下载开始,Toast.LENGTH\u LONG.show } } 一旦成功,就放弃乐趣{ downloadNotification.onFinishDownload.freeUp.setSuccess.notifyManagertrue } 重写错误消息:字符串{ downloadNotification.onFinishDownload.freeUp.setErrormessage.notifyManagertrue } 撤销{ 下载通知。取消 } 重写进程进程:Int{ downloadNotification.setProgressprogress.setText$progress%.notifyManagerfalse } } } } } 要使服务保持活动状态,可以使用startForeground API

从:

已启动的服务可以使用startForegroundint通知 API将服务置于前台状态,其中系统 认为这是用户主动意识到的,因此 当内存不足时,不适合杀人。还是 在极端情况下,理论上可能终止服务 来自当前前台应用程序的内存压力,但在 实践这不应该是一个问题


您是否将该服务设置为前台?@m0skit0我已使用startServiceservice,我的最小Api级别低于Api=26。从:已启动的服务可以使用startForegroundint、通知Api将该服务置于前台状态,系统认为它是用户主动意识到的东西,因此在内存不足时不会被杀死。从理论上讲,在当前前台应用程序的极端内存压力下,服务仍然有可能被终止,但实际上这不应该是一个问题。@m0skit0我正在使用一个自定义通知类,是否可以实现此方法?尝试一下,您会看到。