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).