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 ap rogress bar
, how can I do that?
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.
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.xml 1
<service
android:name=".Worker"
>
// here you can also define intent-filter
</service>
Hope it'll help to solve your problem.
1 Otherwise your IntentService never be executed (even if you'll start it).
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.