Using threads and ProgressDialog

本文介绍如何在Android应用中使用ProgressDialog显示等待提示,并通过独立线程计算π至800位数。示例代码展示了如何在主线程外进行耗时计算并更新UI。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 SDK Version:

M5

This is a simple tutorial to show how to create a thread to do some work while displaying an indeterminate ProgressDialog. Click here to download the full source.

We'll calculate Pi to 800 digits while displaying the ProgressDialog. For the sake of this example I copied the "Pi" class from this site.

We start with a new Android project, only thing I needed to change was to give that TextView an id in main.xml so that I could update it in the Activity.

Because this Activity is so small I'll show you the whole thing and then discuss it at the end:

  1. public  class ProgressDialogExample  extends  Activity  implements  Runnable  {
  2. private  String pi_string;
  3. private  TextView tv;
  4. private  ProgressDialog pd;
  5. @Override
  6. public  void onCreate ( Bundle icicle )  {
  7. super. onCreate (icicle );
  8. setContentView ( R. layout. main );
  9. tv =  ( TextView )  this. findViewById ( R. id. main );
  10. tv. setText ( "Press any key to start calculation" );
  11. }
  12. @Override
  13. public  boolean onKeyDown ( int keyCode,  KeyEvent event )  {
  14. pd =  ProgressDialog. show ( this"Working..""Calculating Pi"true,
  15. false );
  16. Thread thread =  new  Thread ( this );
  17. thread. start ( );
  18. return  super. onKeyDown (keyCode, event );
  19. }
  20. public  void run ( )  {
  21. pi_string = Pi. computePi ( 800 ). toString ( );
  22. handler. sendEmptyMessage ( 0 );
  23. }
  24. private  Handler handler =  new  Handler ( )  {
  25. @Override
  26. public  void handleMessage ( Message msg )  {
  27. pd. dismiss ( );
  28. tv. setText (pi_string );
  29. }
  30. };
  31. }

So we see that this Activity implements Runnable. This will allow us to create a run() function to create a thread.

In the onCreate() function on line 18 we find and initialize our TextView and set the text telling the user to press any key to start the computation.

When the user presses a key it will bring us to the onKeyDown() function on line 27. Here we create the ProgressDialog using the static ProgressDialog.show() function, and as a result the pd variable is initialized. We also create a new Thread using the current class as the Runnable object. When we run thread.start() a new thread will spawn and start executing the run() function.

In the run() function we calculate pi and save it to the String pi_string. Then we send an empty message to our Handler object on line 40.

Why use a Handler? We must use a Handler object because we cannot update most UI objects while in a separate thread. When we send a message to the Handler it will get saved into a queue and get executed by the UI thread as soon as possible.

When our Handler receives the message we can dismiss our ProgressDialog and update the TextView with the value of pi we calculated. It's that easy!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值