How to use asynctask to display a progress bar that counts down?
You can do something like this..
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("waiting 5 minutes..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
Then write an async task to update progress..
private class DownloadZipFileTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... urls) {
//Copy you logic to calculate progress and call
publishProgress("" + progress);
}
protected void onProgressUpdate(String... progress) {
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String result) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
This should solve your purpose and it wont even block UI tread..
Android AsyncTask Progress bar
Please try this
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
@Override
protected void onPreExecute()
{
progressDialog= ProgressDialog.show(YourActivity.this, "Progress Dialog Title Text","Process Description Text", true);
//do initialization of required objects objects here
};
@Override
protected Void doInBackground(Void... params)
{
//do loading operation here
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}
You can call this using
LoadData task = new LoadData();
task.execute();
How to add progress bar to standalone Asynctask?
Add a ProgressBar(this is what it's actually called, Spinners are like drop down menus in Android) to the layout of the Activity where you're initializing your AsyncTask.
Then make two functions startProgress()
and stopProgress()
, which start and stop the progress bar.
Give your AsyncTask a reference to the Activity, either by sending it during initialization or execution, or making a function in your asyncTask setActivity(MyActivity activity)
and call it between your AsyncTask initialization and execution.
Override the onPreExecute()
of your AsyncTask to call activity.startProgress()
and onPostExecute()
to call activity.stopProgress()
.
EDIT: You can also try passing a reference to the ProgressBar in the constructor of your AsyncTask. Get the reference to the ProgressBar in the onCreate()
method of your activity, then add it in the AsyncTask constructor. In the onPreExecute()
and onPostExecute()
methods of the AsyncTask, start and stop the progress bars accordingly.
Progressbar not working inside AsyncTask
Reason for progress bar get stuck was my JSON
decoding since onPostExecute
belongs to UI thread it take several time to decode the json results. so it will freeze the UI until JSON decode so move the decoding part to doInBackground
will solve the UI freeze issue since doInBackground
belongs to background thread
Indeterminate ProgressBar not showing during AsyncTask operation
Try to avoid the AsyncTask get() method and use a listener instead.
You should update your code this way:
1) In your googleTranslate class, add a listener:
private Listener listener;
public interface Listener{
void onTaskResult(String string);
}
public void setListener(Listener listener){
this.listener = listener;
}
and call it in your onPostExecute:
@Override
protected void onPostExecute(String s) {
if (listener!=null){ listener.onTaskResult(s); }
mProgressBar.setVisibility(View.GONE);
}
2) update your main class replacing the get with the listener management, replacing this:
AsyncTask<String, Void, String> asyncTask = googleTranslate.execute(vocab, source, target);
try {
String translatedJSON = asyncTask.get();
JSONParser jsonParser = new JSONParser();
String translatedText = jsonParser.parseJSONForTranslation(translatedJSON);
definitionInput.setText(translatedText);
} catch (Exception e) {
dialog.show();
}
with this:
googleTranslate.setListener(new GoogleTranslate.Listener() {
@Override
public void onTaskResult(String string) {
String translatedJSON = string;
JSONParser jsonParser = new JSONParser();
String translatedText = jsonParser.parseJSONForTranslation(translatedJSON);
definitionInput.setText(translatedText);
}
});
googleTranslate.execute(vocab, source, target);
I hope it helped.
ProgressBar countdown with Handlers in an Android application
I guess my method of updating the progress bar can be seen as correct. So for anyone who googles and has the same kind of question: Use the code in the question!
Android Development: seekbar count down!
class Async extends AsyncTask<Void, Integer, Void>{
ProgressDialog dialog;
public Async(Context ctx) {
dialog = new ProgressDialog(ctx);
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
dialog.incrementProgressBy(1);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
dialog.dismiss();
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
int i=0;
while (i < 1000) {
publishProgress(1);
i++;
}
return null;
}
}
ProgressBar in asynctask is not showing on upload
Run on ui thread in asynctask doinbackground() is not correct. Also you are returning null in doInBackground() and you have parameter file_url in onPostExecute(). Return value in doInbackground() recieve value in onPostExecute().
doInBackGround() runs in background so you cannot access or update ui here.
To update ui you can use onPostExecute().
Your AsyncTask should be something like below. You are doing it the wrong way.
http://developer.android.com/reference/android/os/AsyncTask.html. See the topic under The 4 steps
pd= new ProgressDialog(this);
pd.setTitle("Posting data");
new PostTask().execute();
private class PostTask extends AsyncTask<VOid, Void, Void> {
protected void onPreExecute()
{//display dialog.
pd.show();
}
protected SoapObject doInBackground(Void... params) {
// TODO Auto-generated method stub
//post request. do not update ui here. runs in background
return null;
}
protected void onPostExecute(Void param)
{
pd.dismiss();
//update ui here
}
Related Topics
Outofmemory Exception When Loading Bitmap from External Storage
Garbage Collection Causes:Mediaplayer Finalized Without Being Released
How to Detect App Removed from the Recent List
Getactionview() of My Menuitem Return Null
How to Create/Write File in the Root of the Android Device
How to Move Main Content with Drawer Layout Left Side
How to Allocate These Folders in Another Place
How to Programmatically Log Out from Facebook Sdk 3.0 Without Using Facebook Login/Logout Button
How to Set an Animated Gif File as Live Wallpaper in Android
Post on Facebook Wall Using Facebook Android Sdk Without Opening Dialog Box
Center Align Title in Action Bar Using Styles in Android
Android: Skimagedecoder:: Factory Returned Null
The Import Android.Support.V7.App Cannot Be Resolved
Multiple Dex Files Define Lcom/Google/Android/Gms/Internal/Zzau
Recycle-View Inflating Different Row :- Getting Exception While Binding the Data
Differencebetween the States Selected, Checked and Activated in Android
Android Multiple Screen Sizes with Same Density
Android: How Handle Message Error from the Server Using Volley