Android 后台线程实例
Android 后台线程实例
所有的Android应用程序组件——包括Activity、Service和Broadcast Receiver,都运行在主线程。因此,任何组件中的耗时操作都将阻塞其它的组件,包括Service和可见的Activity。不仅仅是你要避免这种情况,甚至是你都不应该能遇到这种情况。为耗时的处理使用后台线程,包括文件操作、网络搜索、数据库交互和复杂的计算。
创建新的线程
你可以使用Android中的Handler类和java.lang.Thread中的线程类来创建和管理子线程。下面的框架代码显示了如何把操作移到子线程中:
java代码:
- // This method is called on the main GUI thread.
- private void mainProcessing() {
- // This moves the time consuming operation to a child thread.
- Thread thread = new Thread(null, doBackgroundThreadProcessing, “Background”);
- thread.start();
- }
- // Runnable that executes the background processing method.
- private Runnable doBackgroundThreadProcessing = new Runnable() {
- public void run() {
- backgroundThreadProcessing();
- }
- };
- // Method which does some processing in the background.
- private void backgroundThreadProcessing() {
- [ ... Time consuming operations ... ]
- }
为GUI操作同步线程
无论何时你在GUI环境中使用后台线程,在创建和修改图形组件之前,同步子线程和主线程是很重要的。
Handler类允许你向创建Handler类的线程委派一个方法。使用Handler类,你可以借用post方法来从后台线程通知更新UI。下面的例子显示了使用Handler来更新GUI线程的框架代码:
- // Initialize a handler on the main thread.
- private Handler handler = new Handler();
- private void mainProcessing() {
- Thread thread = new Thread(null, doBackgroundThreadProcessing, “Background”);
- thread.start();
- }
- private Runnable doBackgroundThreadProcessing = new Runnable() {
- public void run() {
- backgroundThreadProcessing();
- }
- };
- // Method which does some processing in the background.
- private void backgroundThreadProcessing() {
- [ ... Time consuming operations ... ]
- handler.post(doUpdateGUI);
- }
- // Runnable that executes the update GUI method.
- private Runnable doUpdateGUI = new Runnable() {
- public void run() {
- updateGUI();
- }
- };
- private void updateGUI() {
- [ ... Open a dialog or modify a GUI element ... ]
- }
对于修改View的特殊情形,UIThreadUtilities类提供了runOnUIThread方法,来让你迫使一个方法运行在和特定View、Activity或对话框相同的线程上。
在程序组件中,Notification和Intent总是在GUI线程中接收和处理的。对于其他的情况,如与GUI线程中创建的对象(例如View)进行显式的交互或显示消息(如Toast),这些操作都必须请求(Invoke)到主线程中执行。
移动EarthquakeService到后台线程中
下面的代码演示了如何移动EarthquakeService中的网络搜索和XML解析的操作到一个后台线程中:
1. 重命名refreshEarthquakes方法为doRefreshEarthquakes。
- private void doRefreshEarthquakes() {
- [ ... previous refreshEarthquakes method ... ]
- }
- private void refreshEarthquakes() {
- Thread updateThread = new Thread(null, backgroundRefresh,“refresh_earthquake”);
- updateThread.start();
- }
- private Runnable backgroundRefresh = new Runnable() {
- public void run() {
- doRefreshEarthquakes();
- }
- };
评论暂时关闭