In my Project I want to parallelly load the data from 3 different url's(webservice) while loading the page.
for this I use 3 different asynctask to load data.
I want to show the progress dialog message as "loading" when first asynctask is started and close dialog after last asynctask is completed(until complete 3 tasks show progress dialog).
Please tell me the way to handle this situation.
There are different ways to handle this situation.
You can use a counter variable initialized with the number of tasks, and this counter gets decremented when an AsyncTask is complete. When the counter is 0, the ProgressDialog is dismissed. You can do this decrement and checking for progress dialog dismissal in each of the AysncTask's onPostExecute.
You may not need the 3 different AsyncTasks. You can use a single AsyncTask with a CountDownLatch for Thread synchronization.
One way you can do is you can create an interface OnThreadFinishListener (with an onThreadFinish() method) and have your activity implement it. All of your AsyncTask should register itself to the listener so each time a thread finishes it calls the onThreadFinish method. in the onThreadFinish method it should check if there is still a thread running in the background (something like if (asyncTask1.getStatus() == AsyncTask.Status.RUNNING || asyncTask2.getStatus() == AsyncTask.Status.RUNNING ... ) )
If all are false then you can dismiss the progress dialog.
You may do it as below:
//define progress dialog object globally in your parent class
ProgressDialog pd = null;
AsyncTask1 onPreExecute:
pd = ProgressDialog.show(this, "title", "loading", true);
AsyncTask2:
//do nothing
AsyncTask3 onPostExecute:
if(pd!=null)
pd.dismiss();
Related
I use a code that access HTTP service to receive login data, and before it, I tried to show a processing dialog, but it did not showed up. Then I tried to simply change the button label to tell the user to wait until data returns, but it also did not work. That's my code:
tryingToLoginDialog = ProgressDialog.show(this, "Por Favor Aguarde", "Efetuando Login...", true);
btn_Entrar.setText("Wait while logins...");
btn_Entrar.invalidate();
App.webService.Login(txtLogin.getText().toString(), txtSenha.getText().toString());
String LoginUserData = App.webService.getUserData(); /* this method freezes the app but do not crash it cause ThreadPolicy permit all*/
/* here some if's */
tryingToLoginDialog.dismiss();
btn_Entrar.setText("Login");
btn_Entrar.invalidate();
You should use AsynTask. This has 4 different method doInBackground() This method run in background(not in UI Thread) In your case you can do your networking stuff here and other three methods are onProgressUpdate(), onPostExecute() and onPreExecute() run in UI Thread So you can use these function to update GUI.
You were saying you want to show dialog so setup progressdialog in onPreExecute() and do your freezing stuff in doInBackground() and use other method according to your need.
See detail here
I have an activity with an inner (asynctask) class.
I'm trying to create a progress dialog ,and in a "for" loop create several instances of my inner (asynctask) class.
those instances supposed to update the message + progress of the progress dialog.
I created the progress dialog inside the activity and showed it inside the
asynctask->onProgressUpdate function.
however the progress dialog is not showing up.
Can anyone tell me if there's another way to update the UI ??
Run AsyncTask successively, one by one, and update your dialog on preExecute or postExecute. And write your for loop in doInBackground, where i is a indicator what task executed.
I have loadData() which used to load my Neuroph network.
I want to run it with a progress dialog but if part of loadData() execute i increase my progress dialog.
I need tutorial for it.
I know Thread with Handler, AsyncTask. but cannot do that with these two methods.
So i recommend to you use meant AsyncTask that is specified for updating UI with some progress. This tool contains three the most important methods:
doInBackground - It's doing your work in background Thread.
onProgressUpdate - For updating your UI(in your case here you can incrementing your progress for ProgressDialog)
onPostExecute - It's called after your work finished.
Then, AsyncTask is strong tool, is very efficient and also using generics so is type-safe. But you need to read more about it so first have look at reference and then i recommend to you have look at Android Threads, Handlers and AsyncTask on Vogella, it's great source.
Then you want to use ProgressDialog.
So simply create it and show() it before you execute AsyncTask. Then in onPostExecute method you just call dismiss method for dismissing ProgressDialog.
ProgressDialog pd = new ProgressDialog(this);
pd.setProgress(0);
pd.setTitle("Some title");
pd.setMessage("Some message...");
pd.show();
yourTask.execute(); // here you are executing your AsyncTask
Then in your OnPostExecute method just call dismiss:
#Override
protected void onPostExecute(Void unused) {
pd.dismiss(); // dismissing your ProgressDialog
}
I hava an activity which might have to load data when it's starting up.
I've read some other questions and they all involved passing the progressdialog to the asycntask (like this one which has to load the data and close it after it's done. this is no option for my application since the method getNewData starts up 5 asynctasks and returns when they're all done.
Anybody has an idea how i can make sure the progressdialog is shown? atm the screen just stays black until the application is done loading.
ProgressDialog pd = ProgressDialog.show(this, "Loading...", "Loading data", true);
dataCommunicator.getNewData(this);
pd.dismiss();
You may have a main AsyncTask inside which you can have the other threads running in doInBackground(). Show the progressDialog in this main AsyncTask onPreExecute().
Maintain an int variable which can be accessed across the child AsyncTask and update its value as the child completes its operations. When that int is equal to 5 return from the main AsyncTask doInBackground() and dismiss the progressDialog in onPostExecute().
I want to make a Progress Dialog Box in my app to use when sending some information. But the code I wrote won't work. It the method send() executes but the dialog box never appears because it dismisses very quickly
Here is my code :
ProgressDialog myProgressDialog = ProgressDialog.show(Tents.this,
"Please wait...", "Sending...", true);
send();
myProgressDialog.dismiss();
goFour();
How do I make the Dialog Box Last a little longer?
First of all - you should not do send() in the same thread as show() and dismiss() - because you are effectively blocking UI thread during sending. The dialog will actually never show - because in order to show it after show() is called, you need to give the control back to the main looper in UI thread and simply finish handling whatever event you are handling. Otherwise the UI thread will never have a chance to draw your dialog.
The best idea is to start running send() in AsyncTask and call dismiss() in onPostExecute() (see http://developer.android.com/reference/android/os/AsyncTask.html to get idea how to run async task).
You are probably getting a progress dialog, but having it immediately dismiss as it has nothing to wait for.
I'll pretend you want this in OnCreate for my example:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProgressDialog pd = ProgressDialog.show(this, "Please wait...", "Sending...");
new Thread(new Runnable(){
public void run() {
send();
pd.dismiss();
}
}).start();
gofour();
}
EDIT: If it still goes away immediately, make sure send(); does something that actually takes some time. ;)
The UI thread is used to start send() , this will not work and progress dialog will not be shown .
Call send in another thread or AsynTask doBackground and on completion dismiss the dialog.
If your send action is completing so quickly that the dialog is not displaying properly, might I suggest instead using an indeterminate progress bar in the upper right corner of your activity via requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) and then utilizing setProgressBarIndeterminateVisibility(true/false).
"I guess my real question would then be how do I make it so that it lasts a little longer?" My answer would be WHY???!!!
I think you would be better showing an alert dialog to confirm your send function has completed, it would be annoying for the user having to wait for no reason!