Using threads and ProgressDialog
来源:互联网 发布:手机远程拍照软件 编辑:程序博客网 时间:2024/09/21 09:18
SDK Version:
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:
- public class ProgressDialogExample extends Activity implements Runnable {
- private String pi_string;
- private TextView tv;
- private ProgressDialog pd;
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- tv = (TextView) this.findViewById(R.id.main);
- tv.setText("Press any key to start calculation");
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- pd = ProgressDialog.show(this, "Working..", "Calculating Pi", true,
- false);
- Thread thread = new Thread(this);
- thread.start();
- return super.onKeyDown(keyCode, event);
- }
- public void run() {
- pi_string = Pi.computePi(800).toString();
- handler.sendEmptyMessage(0);
- }
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- pd.dismiss();
- tv.setText(pi_string);
- }
- };
- }
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!
- Using threads and ProgressDialog
- Using User-Interface Threads
- Using Worker Threads
- Using Threads for Parallelism
- Using Worker Threads
- Processes and Threads Sample
- Threads and Collections
- Processes and Threads
- Threads and Swing
- Threads and Swing
- Android------Process and Threads
- Processes and Threads
- Processes and Threads
- Android Processes and Threads
- Processes and Threads
- Processes and Threads
- Processes and Threads
- Processes and Threads
- Android Development Demo: “DialANumber”
- Moto Linux应用开发(-): Ubuntu上建立Moto E6交叉编译工具链
- 房产投资小技巧
- UML基础: 统一建模语言简介
- JVM调优总结
- Using threads and ProgressDialog
- 发挥青年创业的人才智慧 湖塘镇的青年企业家商会终于成立
- Excel导入SQL数据库
- 基于java的mud游戏CoffeeMud(1)--- 安装
- Separating Lists with Headers
- 下载excel模板
- 闲极无聊,开始学JAVA
- Getting Data From the Web (URLConnection via http)
- cookie总结