Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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一个接一个地下载多个文件,并在ListView中显示进度_Android_Android Listview_Android Asynctask_Intentservice_Android Download Manager - Fatal编程技术网

Android一个接一个地下载多个文件,并在ListView中显示进度

Android一个接一个地下载多个文件,并在ListView中显示进度,android,android-listview,android-asynctask,intentservice,android-download-manager,Android,Android Listview,Android Asynctask,Intentservice,Android Download Manager,用户可以添加任意数量的下载,但下载将一个接一个地开始,即下一次下载仅在当前下载完成后开始。用户将导航离开显示下载进度的活动以添加新的下载,并且当添加要下载的新文件时,应用程序将导航回显示下载进度的活动,其中将显示先前添加的下载进度,并将当前添加的文件保留为挂起下载。下载完成后,挂起的下载将开始下载。 通过这种方式,用户可以添加任意数量的下载,它们将一个接一个地开始。我想在后台一个接一个地连续下载它们。我想在列表视图中显示进度和状态。因此,ListView看起来像: 文件1…正在进行中,表示为39

用户可以添加任意数量的下载,但下载将一个接一个地开始,即下一次下载仅在当前下载完成后开始。用户将导航离开显示下载进度的活动以添加新的下载,并且当添加要下载的新文件时,应用程序将导航回显示下载进度的活动,其中将显示先前添加的下载进度,并将当前添加的文件保留为挂起下载。下载完成后,挂起的下载将开始下载。 通过这种方式,用户可以添加任意数量的下载,它们将一个接一个地开始。我想在后台一个接一个地连续下载它们。我想在列表视图中显示进度和状态。因此,ListView看起来像:

文件1…正在进行中,表示为39%

文件2…挂起

文件3。。。未决


文件4。。。待定

我建议使用IntentService:

public class FileDownloader extends IntentService {

private static final String TAG = FileDownloader.class.getName();



public FileDownloader() {
    super("FileDownloader");
}

@Override
protected void onHandleIntent(Intent intent) {
    String fileName = intent.getStringExtra("Filename");
    String folderPath = intent.getStringExtra("Path");
    String callBackIntent = intent
            .getStringExtra("CallbackString");

     // Code for downloading

     // When you want to update progress call the sendCallback method

}

private void sendCallback(String CallbackString, String path,
        int progress) {

        Intent i = new Intent(callBackIntent);
        i.putExtra("Filepath", path);
        i.putExtra("Progress", progress);
        sendBroadcast(i);

}

}
然后,要开始下载文件,只需执行以下操作:

Intent i = new Intent(context, FileDownloader.class);
i.putExtra("Path", folderpath);
i.putExtra("Filename", filename);
i.putExtra("CallbackString",
            "progress_callback");
startService(i);
现在,您应该像处理任何其他广播一样处理“progress\u callback”回调,注册接收器等。在本例中,使用文件路径来确定哪个文件应该更新其进度

不要忘记在您的清单中注册服务

 <service android:name="yourpackage.FileDownloader" />
请记住在
ondestory
方法中运行此操作:

@Override
protected void onDestroy() {
  super.onDestroy();
  unregisterReceiver(receiver);
}
请注意,“progress\u callback”可以是您选择的任何其他字符串


示例代码借用自

考虑使用AsyncTask,但不知道如何在当前下载完成后启动下一个挂起下载的进度条。以及如何在用户导航到另一个活动后更新进度条,以便添加新的下载。您能否提供有关CallbackString的更多信息?它到底是什么?我应该如何使用它?善意的提醒!API 30中不推荐使用IntentService。
@Override
protected void onDestroy() {
  super.onDestroy();
  unregisterReceiver(receiver);
}