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 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:
-
-
-
-
-
-
-
@Override
-
publicvoidonCreate
(Bundleicicle
){
-
super.onCreate(icicle);
-
setContentView
(R.
layout.
main);
-
-
-
tv.setText("Press any key to start calculation");
-
}
-
-
@Override
-
publicbooleanonKeyDown
(intkeyCode,
KeyEventevent
){
-
-
-
false);
-
-
-
thread.start();
-
-
returnsuper.onKeyDown(keyCode, event);
-
}
-
-
publicvoidrun(){
-
pi_string = Pi.computePi(800).toString();
-
handler.sendEmptyMessage(0);
-
}
-
-
-
@Override
-
publicvoidhandleMessage
(Messagemsg
){
-
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!
分享到:
相关推荐
Threads and the Concurrency Utilities helps all Java developers master and use these capabilities effectively. This book is divided into two parts of four chapters each. Part 1 focuses on the Thread...
Oracle Solaris 9 操作系统 Threads and Realtime Library 函数手册 Oracle Solaris 9 操作系统是由 Sun Microsystems 公司开发的 Unix 操作系统之一。该操作系统主要面向服务器端市场,具有高度的稳定性、安全性...
Oracle Solaris 9 -man pages section 3:Threads and Realtime Library Functions Oracle Solaris 9 是一个基于 Unix 的操作系统,由 Sun Microsystems 开发。man pages section 3 documentation 中的 Threads and ...
Networking in the Background using AsyncTask and AsyncTaskLoader Summary Chapter 16 Interaction and Animation: Live Wallpaper and Handlers Live Wallpaper Handler Summary Index Colophon
《Java多线程与并发工具》一书深入讲解了Java中线程API和并发工具的使用,这些内容是Java语言中最强大但也最具挑战性的API和语言特性之一。对于初学者而言,利用这些特性编写正确的多线程应用程序通常是非常困难的。...
Threads and the Concurrency Utilities helps all Java developers master and use these capabilities effectively. This book is divided into two parts of four chapters each. Part 1 focuses on the Thread...
New chapters cover thread performance, using threads with Swing, threads and Collection classes, thread pools, and threads and I/O (traditional, new, and interrupted). Developers who cannot yet ...
Java Threads and the Concurrency Utilities 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...
"Threads and Pipes in Console Apps.txt"文档可能对这些概念进行了详细解释,包括线程同步(如锁、信号量和事件)、线程池的使用,以及管道的创建、读写操作和错误处理等。 通过学习这些例子,开发者可以掌握如何...
sdk2003文档 DLLs, Processes, and Threads
Necaise introduces the basic array structure and explores the fundamentals of implementing and using multi-dimensional arrays. The underlying mechanisms of many of Python’s built-in data structures ...
A self-contained reference that relies on the latest UNIX standards,UNIX Systems Programming provides thorough coverage of files, signals,semaphores, POSIX threads, and client-server communication....
Java 19 引入了虚拟线程与平台线程的概念,这是一项旨在提升应用程序效率的重要更新。在本文中,我们将深入探讨这两种线程的差异、工作原理以及如何利用它们来实现更优的可扩展性。 平台线程,通常被称为经典线程,...
《Oracle Solaris 8 man pages section 3: Threads and Realtime Library Functions》是Oracle Solaris 8操作系统中关于线程和实时库函数的官方文档,由Sun Microsystems公司编写。这份文档详细介绍了在Solaris 8...
Unix Systems Programming Communication, Concurrency, and Threads 2003.chm