Original URL: 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 operation, after the completion of the notification main program will progressdialog end.
Here is a demo, very simple to use:
[Java]View Plaincopyprint?
- 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 class Mainactivity extends Activity {
- Private button button;
- private ProgressDialog PD;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Button = (button) Findviewbyid (R.id.button1);
- Button.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- / * Show ProgressDialog * /
- PD = Progressdialog.show (mainactivity. This, "title", "load in, please later ...");
- / * Open a new thread and execute a time-consuming method in the new thread * /
- New Thread (new Runnable () {
- @Override
- public Void Run () {
- Spandtimemethod (); //Time-consuming method
- Handler.sendemptymessage (0); Execute time-consuming method and send to Handler
- }
- }). Start ();
- }
- });
- }
- private void Spandtimemethod () {
- try {
- Thread.Sleep (5000);
- } catch (Interruptedexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- Handler Handler = new Handler () {
- @Override
- public void handlemessage (Message msg) {///handler will execute this method after receiving the message
- Pd.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:
"Turn" my Android Note (10)--progressdialog simple application, waiting for prompt