1

I want to ZIP files which are large, around 500MB or over 1GB also. I have used AsyncTask, but it doesn't work with such large files.

What should I do, to handle such long tasks? I also want to update the UI with a progress bar, how can I do that?

Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
Aritra Roy
  • 15,355
  • 10
  • 73
  • 107

2 Answers2

2

I want to ZIP files which are large, around 500MB or over 1GB also. I have used AsyncTask, but it doesn't work with such large files.

AsyncTask is not designated for longs tasks but for tasks which can last only a few seconds.

What should I do, to handle such long tasks? I also want to update the UI with a progress bar, how can I do that?

For long tasks is usually used IntentService which is also directly designated for this purpose but it cannot provide by it's native implementation way how to communicate with UI Thread.

To solve this problem you can use ResultReceiver you can pass into IntentService via Intent and then it should work.

Quick basic tutorial:

How to create IntentService:

public class Worker extends IntentService {

    public static final int PROGRESS_UPDATE = 324;
    public static final String PROGRESS = "progress";

    @Override
    protected void onHandleIntent(Intent intent) {

        // Get data from Intent
        ResultReceiver r = (ResultReceiver) intent.getParcelableExtra("key");
        ...

        // do your work

        ...

        // send update to caller Activity
        Bundle data = new Bundle();
        data.putExtra(Worker.PROGRESS, value);
        r.send(Worker.PROGRESS_UPDATE, data);
        ...
    }
}

How to create ResultReceiver (in your caller Activity for example):

private class WorkerReceiver extends ResultReceiver {

    public WorkerReceiver(Handler handler) {
       super(handler);
    }

    @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
       super.onReceiveResult(resultCode, resultData);
       if (resultCode == Worker.PROGRESS_UPDATE) {

          int progress = resultData.getInt(Worker.PROGRESS);
          // update your UI with current information from Worker
       }
   }
}

Finally, how to execute IntentService:

Intent i = new Intent(this, Worker.class);
// put some extra data you want
i.putExtra("key", new WorkerReceiver(new Handler()));
...
startService(i);

Finally don't forget to add your IntentService into Manifest.xml1

<service
   android:name=".Worker"
>
// here you can also define intent-filter
</service>

Hope it'll help to solve your problem.


1Otherwise your IntentService never be executed (even if you'll start it).

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • What are its advantages over Executor class (as mentioned above)? – Aritra Roy Jul 20 '14 at 03:52
  • @Aritra I think there are no special advantages. These special classes are i think usually used only of there is no other way how to do it, especially in Android. In Android I'm trying to use only Android build-in classes and components (not derived from Java) as many as possible. – Simon Dorociak Jul 20 '14 at 08:10
1

Why don't you use Executor? AsyncTask is not proper way for long time work. You shoud basically use java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. Please refer Android SDK document about AsyncTask.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

Horyun Lee
  • 1,083
  • 8
  • 9
  • If I use the Executor class, is there any way I can put a progress of my long-running task in the UI? I mean to communicate the progress of the task and update it in the UI. – Aritra Roy Jul 20 '14 at 03:56
  • Sure you can use runOnUiThread method in activity or make Handler and post method. Inside both methods it is the same like publishProgress in AsyncTask. This link mentionwd about post method. http://stackoverflow.com/questions/15136199/when-to-use-handler-post-when-to-new-thread – Horyun Lee Jul 20 '14 at 03:59