Anybody has any idea about ProgressDialog of Andorid with Thread? I am successfully able to display the dialog and dismiss it also, but I have some long logic to be done and then only to dismiss the dialog, until that dialog will display loading message. I put my logic code inside the Run method of thread with While condition that until I didn't get particular result it will run the code. But somehow it is giving Handler and Looper.prepare() error. I try to found this and get to know that I have to use the functionality of Handler class.
ProgressDialog comes under UserInterface.Donot dissmis it inside thread and show before starting thread;
like this.
Thread th=new Thread(this);
ProgressDialog pd=new ProgressDialog(this);
pd.setMessage("Loading...");
pd.show();
th.start();
Now take handler class to dismiss it
void run(){
///do your work;
handler.sendEmptyMessage(0);
}
private Handler handler=new Handler(){
//override onhandle message method and dismiss dialog in it
}
For the same, you can implement AsyncTask concept, and which is very much preferable in Android to implement threading task, by implementing this task, system itself handles the threading task like start, run, stop....etc. and that's why its known as as Painless Threading.
If you have gone through AsyncTask link then i am sure you have noticed those 4 methods of AsyncTask.
For your problem, you can implement the AsyncTask as below:
onPreExecute() - Display progress dialog
doInBackground() - implement all the long running task
onPostExecute() - dismiss progress dialog by calling dialog.dismiss();
I am sure you got it now.
All the best.
Update:
you just need to write Dialog.Show() method inside the onPreExecute() method, and dialog.dismiss() inside the onPostExecute() method. And yes i have told 1000 times that you can't write Toast directly inside the doInBackground() method because Android doesn't allows to ride on the main thread, and if you still want to execute it then write the same Toast message inside the runOnUiThead method (this method should be inside the doInBackground().
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 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 need to do some work on the UI thread, specifically setting up some views, etc. - this can't be done in a background thread. The process is invoked on a button click and takes about a second or so to complete - without a progress dialog it looks as if the app is frozen. I use progress dialog with AsynTasks in several places and it works fine - however here I'm struggling.
I started with simple:
showDialog(DIALOG_PLEASE_WAIT);
viewInfo.setFromGuide(true); //this method just sets a variable
viewInfo.setVenue(venue); //this method does a lot of UI manipulation and takes a second or so
showScreen(VIEW_INFO); //this method shows the corresponding view in ViewFlipper
dismissDialog(DIALOG_PLEASE_WAIT);
However the dialog would not show (sort of expected, as this is all on UI thread.
Then I changed the code to this:
Handler hnd = new Handler() {
#Override
handleMessage(Message m) {
viewInfo.setFromGuide(true);
viewInfo.setVenue(venue);
showScreen(VIEW_INFO);
dismissDialog(DIALOG_PLEASE_WAIT);
}
}
showDialog(DIALOG_PLEASE_WAIT);
new Thread() {
public void run() {
hnd.sendEmptyMessage(0);
}
}.start();
This still doesn't show the dialog - naturally, the UI work in handleMessage is still done on the UI thread. So, what can I do to show the progress dialog?
If it takes really takes a second or so to complete than maybe you can just use a simple Toast notification with a message like "Please wait"
as you are using AsyncTask you can override onProgressUpdate which is called when ever you call publishProgress() from inside the doInBackGround so you can publish your results smoothly while working in background because, onProgressUpdate works on the UI thread.
I'm connecting to bluetooth in background and I want to show alert or toast befor processing socket.
Here is my code
mmSocket.connect();
connectionHandler.post(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(BluetoothSampleActivity.this);
builder.setMessage("conneted");
builder.show();
}
});
manageConnectedSocket(mmSocket);
but as I understand alert will be shown only when this will end his work. How can i show alert BEFORE executing manageConnectedSocket(mmSocket);?
P.S.: I tryed runOnUiThread - it didn't help
I suggest to move your logic from a normal Thread to an AsyncTask. Whenever you have to do heavy work but also to interact with the UI, AsyncTask is the best practice.
On the preExecute() method you show the dialog or update UI, on doInBackground you do whatever you need to do and on onPostExecute you dismiss the dialog or re-update the UI.
Here the AsyncTask doc.
En plus, if you need to show a progress of the work that you are doing in background, it comes super easy thanks to the methods publishProgress/onProgressUpdate.
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!