[ACCEPTED]-android how to work with asynctasks progressdialog-android
onProgressUpdate()
is used to operate progress of asynchronous 5 operations via this method. Note the param 4 with datatype Integer
. This corresponds to the 3 second parameter in the class definition. This 2 callback can be triggered from within the 1 body of the doInBackground()
method by calling publishProgress()
.
Example
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AsyncTaskExample extends Activity {
protected TextView _percentField;
protected Button _cancelButton;
protected InitTask _initTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_percentField = (TextView) findViewById(R.id.percent_field);
_cancelButton = (Button) findViewById(R.id.cancel_button);
_cancelButton.setOnClickListener(new CancelButtonListener());
_initTask = new InitTask();
_initTask.execute(this);
}
protected class CancelButtonListener implements View.OnClickListener {
public void onClick(View v) {
_initTask.cancel(true);
}
}
/**
* sub-class of AsyncTask
*/
protected class InitTask extends AsyncTask<Context, Integer, String> {
// -- run intensive processes here
// -- notice that the datatype of the first param in the class definition matches the param passed to this
// method
// -- and that the datatype of the last param in the class definition matches the return type of this method
@Override
protected String doInBackground(Context... params) {
// -- on every iteration
// -- runs a while loop that causes the thread to sleep for 50 milliseconds
// -- publishes the progress - calls the onProgressUpdate handler defined below
// -- and increments the counter variable i by one
int i = 0;
while (i <= 50) {
try {
Thread.sleep(50);
publishProgress(i);
i++;
}
catch (Exception e) {
Log.i("makemachine", e.getMessage());
}
}
return "COMPLETE!";
}
// -- gets called just before thread begins
@Override
protected void onPreExecute() {
Log.i("makemachine", "onPreExecute()");
super.onPreExecute();
}
// -- called from the publish progress
// -- notice that the datatype of the second param gets passed to this method
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("makemachine", "onProgressUpdate(): " + String.valueOf(values[0]));
_percentField.setText((values[0] * 2) + "%");
_percentField.setTextSize(values[0]);
}
// -- called if the cancel button is pressed
@Override
protected void onCancelled() {
super.onCancelled();
Log.i("makemachine", "onCancelled()");
_percentField.setText("Cancelled!");
_percentField.setTextColor(0xFFFF0000);
}
// -- called as soon as doInBackground method completes
// -- notice that the third param gets passed to this method
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("makemachine", "onPostExecute(): " + result);
_percentField.setText(result);
_percentField.setTextColor(0xFF69adea);
_cancelButton.setVisibility(View.INVISIBLE);
}
}
}
The 4 steps
When an asynchronous task is executed, the 32 task goes through 4 steps:
onPreExecute(), invoked on the 31 UI thread before the task is executed. This 30 step is normally used to setup the task, for 29 instance by showing a progress bar in the 28 user interface.
doInBackground(Params...), invoked on the background 27 thread immediately after onPreExecute() finishes 26 executing. This step is used to perform 25 background computation that can take a long 24 time. The parameters of the asynchronous 23 task are passed to this step. The result 22 of the computation must be returned by this 21 step and will be passed back to the last 20 step. This step can also use publishProgress(Progress...) to 19 publish one or more units of progress. These 18 values are published on the UI thread, in 17 the onProgressUpdate(Progress...) step.
onProgressUpdate(Progress...), invoked 16 on the UI thread after a call to publishProgress(Progress...). The 15 timing of the execution is undefined. This 14 method is used to display any form of progress 13 in the user interface while the background 12 computation is still executing. For instance, it 11 can be used to animate a progress bar or 10 show logs in a text field.
onPostExecute(Result), invoked on the 9 UI thread after the background computation 8 finishes. The result of the background computation 7 is passed to this step as a parameter.
example
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
AsyncTask's generic types The 6 three types used by an asynchronous task 5 are the following:
Params, the type of the parameters 4 sent to the task upon execution.
Progress, the type 3 of the progress units published during the 2 background computation.
Result, the type of the 1 result of the background computation.
Yes, you are right, there are four method 26 in AsyncTask
When an asynchronous task is executed, the 25 task goes through 4 steps:
onPreExecute()
Invoked on the 24 UI thread immediately after the task is executed. This 23 step is normally used to setup the task, for 22 instance by showing a progress bar in the 21 user interface.
doInBackground(Params...)
Invoked on the background thread immediately 20 after onPreExecute()
finishes executing. This step is 19 used to perform background computation that 18 can take a long time. The parameters of 17 the asynchronous task are passed to this 16 step. The result of the computation must 15 be returned by this step and will be passed 14 back to the last step. This step can also 13 use publishProgress(Progress...)
to publish one or more units of progress. These 12 values are published on the UI thread, in 11 the onProgressUpdate(Progress...)
step.
onProgressUpdate(Progress...)
Invoked on the UI thread after a call to 10 publishProgress(Progress...)
. The timing of the execution is undefined. This 9 method is used to display any form of progress 8 in the user interface while the background 7 computation is still executing. For instance, it 6 can be used to animate a progress bar or 5 show logs in a text field.
onPostExecute(Result)
Invoked on the 4 UI thread after the background computation finishes. The 3 result of the background computation is 2 passed to this step as a parameter.
For more 1 inforamtion click here
onProgressUpdate runs on the UI thread after publishProgress is invoked. From 2 AsyncTask documentation - your code should 1 look something like this
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
hey this might help you??
the progress bar will automatically disappear 1 when you will get the response
User_AsyncTaskk extends AsyncTask
public class User_AsyncTask extends AsyncTask<String, String, String>
{
String response = "";
@Override
protected void onPreExecute()
{
try
{
if (progressDialog != null)
progressDialog.cancel();
}
catch (Exception e)
{
}
progressDialog = ProgressDialog.show(DisplayDetails.this, "", "Please wait...", true, true);
progressDialog.setCancelable(false);
progressDialog.show();
}
protected String doInBackground(String... params)
{
try
{
//Complete ur Code
Log.i("AUTO ", "response is : " + response);
return response;
}
catch (Exception e)
{
}
}
@Override
protected void onPostExecute(String s)
{
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
try {
}
catch (Exception e)
{
}
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.