Transferred from: http://blog.csdn.net/barryhappy/article/details/7376231
In the application often use some time-consuming operation, need users to wait, such as loading Web content ...
At this point you need a hint to tell the user that the program is being executed, and that there is no animation or real death ... I'm so embarrassed ...
and ProgressBar, ProgressDialog and so on are specialized to do this.
Take ProgressDialog as an example, the general use of it is: before the time-consuming operation pops up ProgressDialog prompt the user, and then open a new thread, in the new thread to perform time-consuming operations, Notifies the main program to end ProgressDialog after execution is complete.
Here is a demo, very simple to use:
Package Com.android.ui; Import android.app.Activity; Import Android.app.ProgressDialog; Import Android.os.Bundle; Import Android.os.Handler; Import Android.os.Message; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Public classMainactivity extends Activity {Privatebutton button; PrivateProgressDialog PD; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Button=(Button) Findviewbyid (R.id.button1); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {/*Show ProgressDialog*/PD= Progressdialog.show (mainactivity. This,"title","load, please later ..."); /*start a new thread and execute a time-consuming method in a new thread*/ NewThread (NewRunnable () {@Override Public voidrun () {Spandtimemethod ();//time-consuming approachHandler.sendemptymessage (0);//execute time-consuming method and send to Handler}}). Start (); } }); } Private voidSpandtimemethod () {Try{Thread.Sleep ( the); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} Handler Handler=NewHandler () {@Override Public voidHandlemessage (Message msg) {//handler will execute this method when it receives the messagePd.dismiss ();//Close ProgressDialog } }; }
Main.xml only a button, do not post, the program should be well understood, click the button to pop up ProgressDialog, in the new thread to perform time-consuming operation (Thread.Sleep (5000);), after the completion of the notification handler, End ProgressDialog.
The results are as follows:
My Android Notes (10)--progressdialog simple application, Waiting for hints (reprint)