"Turn" my Android Note (10)--progressdialog simple application, waiting for prompt

Source: Internet
Author: User

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?
  1. Package com.android.ui;
  2. Import android.app.Activity;
  3. Import Android.app.ProgressDialog;
  4. Import Android.os.Bundle;
  5. Import Android.os.Handler;
  6. Import Android.os.Message;
  7. Import Android.view.View;
  8. Import Android.view.View.OnClickListener;
  9. Import Android.widget.Button;
  10. Public class Mainactivity extends Activity {
  11. Private button button;
  12. private ProgressDialog PD;
  13. @Override
  14. public void OnCreate (Bundle savedinstancestate) {
  15. super.oncreate (savedinstancestate);
  16. Setcontentview (R.layout.main);
  17. Button = (button) Findviewbyid (R.id.button1);
  18. Button.setonclicklistener (new Onclicklistener () {
  19. @Override
  20. public void OnClick (View v) {
  21. / * Show ProgressDialog * /
  22. PD = Progressdialog.show (mainactivity.   This, "title", "load in, please later ...");
  23. / * Open a new thread and execute a time-consuming method in the new thread * /
  24. New Thread (new Runnable () {
  25. @Override
  26. public Void Run () {
  27. Spandtimemethod (); //Time-consuming method
  28. Handler.sendemptymessage (0); Execute time-consuming method and send to Handler
  29. }
  30. }). Start ();
  31. }
  32. });
  33. }
  34. private void Spandtimemethod () {
  35. try {
  36. Thread.Sleep (5000);
  37. } catch (Interruptedexception e) {
  38. //TODO auto-generated catch block
  39. E.printstacktrace ();
  40. }
  41. }
  42. Handler Handler = new Handler () {
  43. @Override
  44. public void handlemessage (Message msg) {///handler will execute this method after receiving the message
  45. Pd.dismiss (); //Close ProgressDialog
  46. }
  47. };
  48. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.