简体   繁体   English

service()或下载管理器中的Asynctask从服务器下载文件

[英]Asynctask within service() or download manager to download the files from server

I have a listView and and click of each row I have to download an mp3 file. 我有一个listView,然后单击每一行,我必须下载一个mp3文件。 I'm confused between AsyncTask within service() and build in DownloadManager (above API 9). 我对service()中的AsyncTask和内置于DownloadManager(高于API 9)的内部感到困惑。

However because you handle each call to onStartCommand() yourself, you can perform multiple requests simultaneously. 但是,由于您自己处理对onStartCommand()每次调用, onStartCommand()可以同时执行多个请求。 That's not what this example does, but if that's what you want, then you can create a new thread for each request and run them right away (instead of waiting for the previous request to finish). 这不是本示例的工作,但是如果您要这样做,则可以为每个请求创建一个新线程并立即运行它们(而不是等待上一个请求完成)。

From above line from docs I am thinking to create new instance of AsyncTask in the onStartCommand method, so each file will be downloaded as a separate task, but while searching I came across DownloadManagerr. 从文档的上一行,我想在onStartCommand方法中创建AsyncTask新实例,因此每个文件将作为一个单独的任务下载,但是在搜索时遇到了DownloadManagerr。 I found very little information about it in the documentation. 我在文档中发现的信息很少。

I'm having doubts about DownloadManager. 我对DownloadManager有疑问。 Does it is work like IntentService? 它像IntentService一样工作吗? Someone please guide me what to use (AsyncTask within service() or DownloadManager) and please clear up the above doubts. 有人请指导我使用什么(service()或DownloadManager中的AsyncTask),并清除上面的疑问。

Asynktask within service() service()中的Asynktask

Implementing this code is much easier because you just create a service and start it by calling 实施此代码要容易得多,因为您只需创建服务并通过调用来启动它

startService(my_service);

But as begin service it can be stop by android when low on memory (your task will fail) It can be manually shutdown By user by going to setting and third It can be shut off if the phone is switched off you cannot resume your task after boot 但是作为开始服务,当内存不足时它可以由android停止(您的任务将失败),可以通过设置进行手动关闭,然后由用户手动关闭。如果手机关机,则可以关闭它。靴

DownloadManager 下载管理器

With DownloadManager you have to setup a BrodcastReceiver ACTION_NOTIFICATION_CLICKED but you get more control over your download as this API was solely developed for downloading purpose 使用DownloadManager时,您必须设置BrodcastReceiver ACTION_NOTIFICATION_CLICKED,但由于此API专为下载目的而开发,因此您可以更好地控制下载

Pros: 优点:

  1. Download Can be restart after your cell has rebooted. 下载可以在单元重启后重新启动。
  2. Can limit use over mobile networks (force wifi download only). 可以限制通过移动网络的使用(仅强制wifi下载)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM