Android 后台线程实例

所有的Android应用程序组件——包括Activity、ServiceBroadcast 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 ... ]
} 

Handler类允许你延迟委派或在特定的时间来执行,使用postDelayed和postAtTime方法。

        对于修改View的特殊情形,UIThreadUtilities类提供了runOnUIThread方法,来让你迫使一个方法运行在和特定View、Activity或对话框相同的线程上。

        在程序组件中,Notification和Intent总是在GUI线程中接收和处理的。对于其他的情况,如与GUI线程中创建的对象(例如View)进行显式的交互或显示消息(如Toast),这些操作都必须请求(Invoke)到主线程中执行。
移动EarthquakeService到后台线程中

        下面的代码演示了如何移动EarthquakeService中的网络搜索和XML解析的操作到一个后台线程中:

        1. 重命名refreshEarthquakes方法为doRefreshEarthquakes。

private void doRefreshEarthquakes() {
[ ... previous refreshEarthquakes method ... ]
} 
 2. 创建一个新的refreshEarthquakes方法。它需要启动一个执行新命名的doRefreshEarthquakes方法的线程。

private void refreshEarthquakes() {
Thread updateThread = new Thread(null, backgroundRefresh,“refresh_earthquake”);
updateThread.start();
}

private Runnable backgroundRefresh = new Runnable() {
public void run() {
doRefreshEarthquakes();
}
}; 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值