I've searched in the web but I only find cases where the users want to show the dialog in the asynctask like this example: protected void onPreExecute().
{
super.onPreExecute();
pDialog = new ProgressDialog(NumericoComercial.this);
pDialog.setMessage("Actualizando ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
What I would like to know is if it can be done an asynctask without showing any dialog in onPreExecute() and onPostExecute() as usually is done.
I have experienced some problems using asynctask with dialogs regarding Window Leaked Error. The thing I've tried is not adding any dialog to the Pre and Post Actions like the following example.
class UpdateCandidatos extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
}
protected void onPostExecute(String file_url) {
}
}
Could be any errors using this method, for example when the activity is finished?
Thank you
onPreExecute() and onPostExecute() are not required methods extending Aynctask then all the things you write in those methods (Dialog, ProgressDialog etc) are unnecessary for the correct working of your class.
class UpdateCandidatos extends AsyncTask<String, String, String> {
protected Long doInBackground(String... urls) {
//add here your background work
}
}
this is a very simplified and fully forking AsyncTask that do "something" in background showing no dialog.
Note: you will have to handle the task according to the lifecycle of your app
I would recommend that you cancel your async task in you onPause() method. This way if your activity closes, the async task wont try to publish any data, and onPostExecute wont be called.
Hm, you shouldn't be having any errors with onPostExecute(), you can use async task without showing any data, its not mandatory. You dont even have to #override that function, just put Void i the declaration ( ...extends AsyncTask<...,...,Void> )
Related
My onStart() event looks like this:
protected void onStart() {
super.onStart();
ShowProgressDialog();
Function1(); //this takes a lot of time to compute
HideProgressDialog();
Function2(); //this function uses the values calculated from Function1
}
But the ProgressDialog wont show.
PS: AsyncTask is not a good solution for my problem because Function2 needs the values calculated from Function1 and I really dont want to chain 4-5 AsyncTasks.
Write the following code onstart
pDialog = new ProgressDialog(this);
pDialog.setMax(5);
pDialog.setMessage("Loading...");
pDialog.setCancelable(true);
pDialog.show();
Seems the activity is not on top of the activtiy stack yet. As can be read in the documentation:
http://developer.android.com/reference/android/app/Activity.html
Also if you do have processing it can block the UI Thread. I would suggest to put it in A A-Sync task. In the asynctask the order is still top to bottom so no need to create multiply a-synctask.
Do this to show the dialog box:
private ProgressDialog inProgressDialog = new ProgressDialog(this);
inProgressDialog.setMessage("In progress...");
inProgressDialog.show();
and to stop dialog:
inProgressDialog.dismiss();
And I think in your case you can give call to function2 using Async task and pass it as params.
You need to run Function1 and Function2 in a background thread. The ProgressDialog will only show once all of OnStart() has finished. So, you need to thread the time consuming code to free up the UI, otherwise the progress dialog won't show.
This is always good practice in Android, if you run time consuming stuff on the main thread the operating system will pester the user with messages about the app being unresponsive.
Some pseudo code:
OnStart()
{
ShowProgressDialog();
StartTimeConsumingThread();
}
Then, in the time consuming thread:
TimeConsumingThread()
{
Function1();
Function2();
RunOnUiThread(
CloseProogressDialog();
)
}
I think for long time calculation it is better to use AsyncTask.
You can do something like this:
private class Task extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MyActivity.this);
pd.show();
}
#Override
protected Void doInBackground(Void... params) {
Function1();
runOnUiThread(new Runnable() {
#Override
public void run() {
pd.dismiss();
}
});
Function2();
return null;
}
}
I'm trying to develop an application to understand android, application delete's default browser's history. Every thing is working fine, I'm using AsyncTask to accomplish the task with ProgressDialog
Here is how I'm deleting the History
ProgressDialog pd;
new AsyncTask<Void, Void, Void>()
{
#Override
protected void onPreExecute()
{
pd = ProgressDialog.show(HistoryClean.this, "Loading..",
"Please Wait", true, false);
}//End of onPreExecute method
#Override
protected Void doInBackground(Void... params)
{
Browser.clearHistory(getContentResolver());
return null;
}//End of doInBackground method
#Override
protected void onPostExecute(Void result)
{
pd.dismiss();
}//End of onPostExecute method
}.execute((Void[]) null);//End of AsyncTask anonymous class
But instead of ProgressDialog I want to implement CircularProgress which it can display the progress value like 10% , 90%....
Some times History may gets deleted faster and some times it may be slow, how to address this problem and dynamically update the CircularProgress bar with Progression Values.
Thanks in advance.
The best two library i found on the net are on github
https://github.com/Todd-Davies/ProgressWheel
https://github.com/f2prateek/progressbutton?source=c
Hope that will help you
AsyncTask has method onProgressUpdate(Progress... values) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress(Progress...).
Refer to the docs for more details
I need to make a transition screen, ou just put a dialog, because the app give a black screen when is creating the database.
I have google, and find some solutions for this. One of then, is just put a progress dialog when the database is been created.
My problem, and newbie question is, where do i put the progress dialog.
A -> BlackScreen -> B where A is the inicial menu, and B the other screen. I have tried to put the dialog on A and/or in B and dont work. So where can i put the code of the progress dialog, so it shows in the BlackScreen ?
Make use of Asyntask . put your database operation of creating database in asyntask in pre execute start dialog post execute cancel dialog in background perform database operation
http://developer.android.com/reference/android/os/AsyncTask.html
For that You have to use Async task :
class DownloadAsyncTask extends AsyncTask<String, String, Void>
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(Login.this, "", "Please Wait ...");
}
#Override
protected Void doInBackground(String... arg0) {
//Do your Task
}
#Override
protected void onProgressUpdate(String...values){
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
progressDialog.dismiss();
}
}
//Create the Object
DownloadAsyncTask downloadAsyncTask = new DownloadAsyncTask();
downloadAsyncTask.execute();
now till your work get's completed it shows progress dialog inside the doInbackground write your logic and onPostExecute dismiss the dialog and call Intent of other Activity.
I am using an AsyncTask to handle complex background operations (compiling a log file to send) and I use a ProgressDialog to show the user progress. I have tried using showDialog() but it never seems to show or dismiss (it is never called), and I followed tutorials on how to do it...
So I am using unmanaged ones, and it won't dismiss my message. I am also wanting to update the message as it starts to compile the log file (as it seems to lag there - or maybe the text view is just really long so it doesn't update like it is supposed to).
I have moved my code around a bit so it look like there are problems (like onProgressUpdate()), but I don't know how to make it work. I have looked around this site and nothing seems to be having the problem I am (not exactly anyways). RunOnUiThread() doesn't work, new Thread(){} doesn't work, and onProgressUpdate() I can't get to work (the documentation is confusing on this).
It also never dismisses. I have set up a listener and it never dismisses.
Does anyone know what is wrong with my code? I thought AsyncTask was supposed to be simple.
private class BuildLogTask extends AsyncTask<Void, Void, String> {
String temp;
ProgressDialog progressdialog = new ProgressDialog(context); //variable is defined at onCreate (held as private, not static)
#Override
protected String doInBackground(Void... params) {
temp = buildLog();
logdata = temp;
publishProgress();
createLogFile();
return temp;
}
protected void onProgressUpdate() {
progressdialog.setMessage("Compiling Log File...");
}
#Override
protected void onPreExecute() {
Log.w(TAG,"Showing Dialog");
send.setEnabled(false);
ProgressDialog progressdialog = new ProgressDialog(context);
progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressdialog.setMessage("Gathering Data...");
progressdialog.setCancelable(false);
progressdialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
Log.e(TAG,"Progress Dialog dismissal.");
}
});
progressdialog.show();
}
#Override
protected void onCancelled(){
Log.e(TAG,"Progress Dialog was Cancelled");
progressdialog.dismiss();
logdata=null;
}
#Override
protected void onPostExecute(String result) {
progressdialog.dismiss();
send.setEnabled(true);
previewAndSend();
}
}
You have two different progress dialogs there, one local to onPreExecute() and one global. The one ur dismissing in your onPostExecution() is your global one which was never actually shown. Remove the local declaration and it should work.
There are two problems.
The signature for on onProgressUpdate is not correct. Try this instead:
#Override
protected void onProgressUpdate(Void... progress) {
progressdialog.setMessage("Compiling Log File...");
}
You're masking the progressDialog member variable with a local variable in onPreExecute()
EDIT: Second problem identified:
Try it,
Replace:
progressdialog.show();
For:
progressdialog = progressdialog.show();
Good luck.
I have developed an android application .In that application getting information from web and displayed in the screen.At the time of getting information i want to load a progress dialog to the screen after getting the information i want dismiss the dialog
Please any one help me how to do this with some sample code
Thanks in advance
You need to implement an AsyncTask.
Example:
class YourAsyncTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
//show your dialog here
progressDialog = ProgressDialog.show(this, "title", "message", true, false)
}
#Override
protected Void doInBackground(Void... params) {
//make your request here - it will run in a different thread
return null;
}
#Override
protected void onPostExecute(Void result) {
//hide your dialog here
progressDialog.dismiss();
}
}
Then you just have to call
new YourAsyncTask().execute();
You can read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html
The point is, you should use two different thread 1st is UI thread, 2nd is "loading data thread"
from the 2nd thread you are to post the process state to the 1st thread, for example: still working or 50% is done
use this to post data