My android notes (10) -- simple application of progressdialog, waiting for prompt

Source: Internet
Author: User

Some time-consuming operations are often used in applications, and users need to wait, such as loading webpage content ......

At this time, you need a prompt to tell the user that the program is being executed, and there is no false or real death ...... ......

Progressbar and progressdialog are dedicated for this purpose.


Take progressdialog as an example. Generally, the following steps are used: The progressdialog prompt is displayed before the time-consuming operation is executed, and a new thread is opened to execute time-consuming operations in the new thread, after the execution is completed, the main program is notified to end progressdialog.

The following is a demo, which is easy 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;
  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. /* Display progressdialog */
  22. Pd = progressdialog. Show (mainactivity. This, "title", "loading... please wait ...... ");
  23. /* Start a new thread and execute time-consuming methods in the new thread */
  24. New thread (New runnable (){
  25. @ Override
  26. Public void run (){
  27. Spandtimemethod (); // time-consuming Method
  28. Handler. sendemptymessage (0); // sends the message to handler after the time-consuming method is executed.
  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. there is only one button in XML and it will not be pasted. The program should be well understood. After clicking the button, progressdialog will pop up and time-consuming operations (thread. sleep (5000);). After the execution is complete, notify handler and end progressdialog.

The running effect is as follows:

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.