Android Download Multiple Files one after another and show progress in ListView

◇◆丶佛笑我妖孽 提交于 2020-01-01 15:53:48

问题


The user can add any number of downloads, but download will start one after another i.e. next download will start only after the completion of present download. The user will navigate away from the activity showing download progress in order to add new downloads, and when new file to be downloaded is added, the application navigates back to the activity showing download progress, where it will show the progress of the download, previously added, and keep the presently added file as pending download. As soon as the downloading completes, pending download will start downloading. In this way the user can add any number of downloads, and they will start one after another. I want to download them serially in background - one after other. I want to show the progress and status in a ListView. So, ListView looks like:

File1 ...in Progress say 39%

File2....Pending

File3... Pending

File4... Pending


回答1:


I would suggest using 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);

}

}

Then to start downloading a file, simply do:

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

Now you should handle the "progress_callback" callback as you would with any other broadcasts, registering the receiver etc. In this example, using the filepath to determine which file should have it's progress visual updated.

Do not forget to register the service in your manifest.

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

Note:

With this solution you can start a service for each file immediately and casually handle incoming broadcasts as each service reports new progress. No need to wait for each file to be downloaded before starting the next. But if you insist on downloading the files in serial, you can of course, by waiting for 100% progress callbacks before invoking the next.

Using 'CallbackString'

You can use it like this in your 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);
}

Remember to run this in the onDestroy method:

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

Note that "progress_callback" could be any other String of your choice.

Example code borrowed from Programmatically register a broadcast receiver



来源:https://stackoverflow.com/questions/19291462/android-download-multiple-files-one-after-another-and-show-progress-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!