我想创建一个在后台下载文件的服务。我已经看过一些样本来做到这一点。当创建服务时,它将检索要下载的URL数组,但我想添加动态URL来提供服务,似乎我应该重新启动服务以获取新的URL。是否有更好的想法同时为下载获取新的URL?
答案 0 :(得分:0)
使用以下方法,它将解决您的问题
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);
}