【问题标题】:Android Download Multiple Files one after another and show progress in ListViewAndroid依次下载多个文件并在ListView中显示进度
【发布时间】:2013-10-10 09:03:55
【问题描述】:

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

File1 ...进行中说 39%

文件2....待处理

文件3...待处理

文件4...待处理

【问题讨论】:

  • 正在考虑使用AsyncTask,但不知道当前下载完成后如何启动下一个待下载的进度条。以及如何在用户导航到另一个活动以添加新下载时保持进度条更新。

标签: android android-listview android-asynctask intentservice android-download-manager


【解决方案1】:

我建议使用 IntentServices:

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_callback”回调。在此示例中,使用文件路径来确定应该更新哪个文件的进度视觉效果。

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

 <service android:name="yourpackage.FileDownloader" />

注意:

使用此解决方案,您可以立即为每个文件启动服务,并在每个服务报告新进度时随意处理传入的广播。在开始下一个文件之前,无需等待下载每个文件。但是如果你坚持要串行下载文件,你当然可以等待 100% 的进度回调,然后再调用下一个。

使用“回调字符串”

您可以在 Activity 中这样使用它:

private BroadcastReceiver receiver;

@Overrride
public void onCreate(Bundle savedInstanceState){

  // your oncreate code

  // starting the download service

  Intent i = new Intent(context, FileDownloader.class);
  i.putExtra("Path", folderpath);
  i.putExtra("Filename", filename);
  i.putExtra("CallbackString",
            "progress_callback");
  startService(i);

  // register a receiver for callbacks 
  IntentFilter filter = new IntentFilter("progress_callback");

  receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      //do something based on the intent's action
      Bundle b = intent.getExtras();
      String filepath = b.getString("Filepath");
      int progress = b.getInt("Progress");
      // could be used to update a progress bar or show info somewhere in the Activity
    }
  }
  registerReceiver(receiver, filter);
}

记得在onDestroy方法中运行这个:

@Override
protected void onDestroy() {
  super.onDestroy();
  unregisterReceiver(receiver);
}

请注意,“progress_callback”可以是您选择的任何其他字符串。

Programmatically register a broadcast receiver借用的示例代码

【讨论】:

  • 您能否提供更多有关 CallbackString 的信息?它实际上是什么,我应该如何使用它?
  • 温馨提醒! IntentService 在 API 30 中已弃用。
猜你喜欢
  • 2013-09-18
  • 1970-01-01
  • 2013-11-21
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
相关资源
最近更新 更多