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 fromthis 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 classProgressDialogExample extends Activity implements Runnable {
  2. private Stringpi_string;
  3. private TextViewtv;
  4. private ProgressDialogpd;
  5. @Override
  6. public voidonCreate ( Bundleicicle ) {
  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 booleanonKeyDown ( intkeyCode, KeyEventevent ) {
  14. pd = ProgressDialog. show ( this, "Working..", "Calculating Pi", true,
  15. false );
  16. Threadthread = new Thread ( this );
  17. thread. start ( );
  18. return super. onKeyDown (keyCode, event );
  19. }
  20. public voidrun ( ) {
  21. pi_string = Pi. computePi ( 800 ). toString ( );
  22. handler. sendEmptyMessage ( 0 );
  23. }
  24. private Handlerhandler = new Handler ( ) {
  25. @Override
  26. public voidhandleMessage ( Messagemsg ) {
  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. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 让Activity变成一个窗口
  2. Android消息机制Message消息池
  3. Android学习资源
  4. Android(安卓)源码分析 —— 从 Toast 出
  5. Android自动打包、签名、优化、上传ANT脚
  6. traceview进行Android性能测试
  7. Android
  8. android ddms查看线程
  9. #Android(安卓)Day2
  10. Android启动Service的两种方式之startSer