在android中使用下载服务的概念
我想创建一个在后台下载文件的服务。我见过一些这样做的样品。创建服务时,它将检索要下载的URL数组,但我想动态添加URL以提供服务,并且似乎应该重新启动服务以获取新的URL。同时获取新URL以供下载更好吗?使用以下方法,它将解决您的问题在android中使用下载服务的概念,android,Android,我想创建一个在后台下载文件的服务。我见过一些这样做的样品。创建服务时,它将检索要下载的URL数组,但我想动态添加URL以提供服务,并且似乎应该重新启动服务以获取新的URL。同时获取新URL以供下载更好吗?使用以下方法,它将解决您的问题 public void startDownload(String URL) { DownloadManager mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE)
public void startDownload(String URL) {
DownloadManager mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(URL);
DownloadManager.Request request = new DownloadManager.Request(
Download_Uri);
// Restrict the types of networks over which this download may
// proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE);
// Set whether this download may proceed over a roaming
// connection.
request.setAllowedOverRoaming(false);
// Set the title of this download, to be displayed in
// notifications (if enabled).
request.setTitle("Question Answer Updates");
// Set a description of this download, to be displayed in
// notifications (if enabled)
request.setDescription("");
// Set the local destination for the downloaded file to a path
// within the application's external files directory
// request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,"CountryList.json");
// Enqueue a new download and same the referenceId
downloadReference downloadReference = mDownloadManager.enqueue(request);
}