Closed houkhan closed 1 year ago
邮件已收到,三日内拜读
我翻看历史issues 尝试这么修改 还是不行的
object GameDownloadManager {
private const val TAG = "GameDownloadManager"
private val downloadMap = mutableMapOf<String, TaskManager>()
fun start(context: Context, downloadUrl: String, gameName: String, gameID: String, isJustDownload: Boolean = false) {
LogUtils.dTag(TAG, "start download downloadUrl==$downloadUrl")
// 校验字段是否合法
if (downloadUrl.isEmpty()) {
AppToast.showShortSafe(R.string.game_download_url_is_empty)
return
}
if (gameName.isEmpty()) {
AppToast.showShortSafe(R.string.game_download_game_name_is_empty)
return
}
if (gameID.isEmpty()) {
AppToast.showShortSafe(R.string.game_download_game_id_is_empty)
return
}
// 下载逻辑
var taskManager = downloadMap[downloadUrl]
if (taskManager == null) {
val taskLimitation = BasicTaskLimitation(1)
taskManager = Task(url = downloadUrl)
.manager(
taskLimitation = taskLimitation,
notificationCreator = GameDownloadNotificationCreator(context, gameID, gameName),
recorder = RoomRecorder()
)
downloadMap[downloadUrl] = taskManager
}
when (taskManager.currentStatus()) {
is Normal -> taskManager.start()
is Pending -> if (!isJustDownload) taskManager.delete()
is Started -> if (!isJustDownload) taskManager.stop()
is Downloading -> if (!isJustDownload) taskManager.stop()
is Failed -> taskManager.start()
is Paused -> taskManager.start()
is Completed -> AppUtils.installApp(taskManager.file())
is Deleted -> taskManager.start()
}
taskManager.subscribe { status ->
// 此处监听下载完成自动触发安装
when (status) {
is Completed -> {
AppUtils.installApp(taskManager.file())
}
}
}
}
fun getDownloadTaskManager(downloadUrl: String): TaskManager? {
return downloadMap[downloadUrl]
}
}
由于我的 通知栏 不能 默认创建 我需要传入不同的参数 我是这么写的 是这个原因创建了多个TaskManager 导致的吗
val taskLimitation = BasicTaskLimitation(1)
这种方式我测试在demo中是正常的 我的是哪里有问题吗
var taskManager = downloadMap[downloadUrl]
if (taskManager == null) {
// **BasicTaskLimitation改成单例,不要每次都创建新的对象**
val taskLimitation = BasicTaskLimitation(1)
taskManager = Task(url = downloadUrl)
.manager(
taskLimitation = taskLimitation,
notificationCreator = GameDownloadNotificationCreator(context, gameID, gameName),
recorder = RoomRecorder()
)
downloadMap[downloadUrl] = taskManager
}
BasicTaskLimitation改成单例,不要每次都创建新的对象
请问下 这个字段是什么作用的 maxConCurrency 这个不是最大并发数吗
maxConCurrency 是控制每个Task内的下载线程数量 taskLimitation 是控制同时下载的Task数量
好的 谢谢解惑 已解决 非常感谢
需求:设置 maxConCurrency = 1 失效了 我设置 maxConCurrency=1 当我多次调用 GameDownloadManager.start 开始多个任务仍会多个同时下载 我应该如何修改呢
我的代码如下