Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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
在android中使用下载服务的概念_Android - Fatal编程技术网

在android中使用下载服务的概念

在android中使用下载服务的概念,android,Android,我想创建一个在后台下载文件的服务。我见过一些这样做的样品。创建服务时,它将检索要下载的URL数组,但我想动态添加URL以提供服务,并且似乎应该重新启动服务以获取新的URL。同时获取新URL以供下载更好吗?使用以下方法,它将解决您的问题 public void startDownload(String URL) { DownloadManager mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE)

我想创建一个在后台下载文件的服务。我见过一些这样做的样品。创建服务时,它将检索要下载的URL数组,但我想动态添加URL以提供服务,并且似乎应该重新启动服务以获取新的URL。同时获取新URL以供下载更好吗?

使用以下方法,它将解决您的问题

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);
    }