Android 使用后台线程

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=102495


  所有的Android应用程序组件——包括Activity、Service和Broadcast Receiver,都运行在主线程.因此,任何组件中的耗时操作都将阻塞其它的组件,包括Service和可见的Activity.

  使用后台线程,对于避免第2章中曾描述的“应用程序无响应”对话框的情况是至关重要的.在Android中定义的无响应是指:Activity在5秒内不响应任何输入事件(例如一个按键事件)和Broadcast Receiver在10秒未完成onReceive的处理操作.

  不仅仅是你要避免这种情况,甚至是你都不应该能遇到这种情况.为耗时的处理使用后台线程,包括文件操作、网络搜索、数据库交互和复杂的计算.

  创建新的线程

  你可以使用Android中的Handler类和java.lang.Thread中的线程类来创建和管理子线程.下面的框架代码显示了如何把操作移到子线程中:

java代码:

  1. // This method is called on the main GUI thread.
  2. private void mainProcessing() {
  3. // This moves the time consuming operation to a child thread.
  4. Thread thread = new Thread(null, doBackgroundThreadProcessing, “Background”);
  5. thread.start();
  6. }

  7. // Runnable that executes the background processing method.
  8. private Runnable doBackgroundThreadProcessing = new Runnable() {
  9. public void run() {
  10. backgroundThreadProcessing();
  11. }
  12. };

  13. // Method which does some processing in the background.
  14. private void backgroundThreadProcessing() {
  15. [ ... Time consuming operations ... ]
  16. }
复制代码

       为GUI操作同步线程

  无论何时你在GUI环境中使用后台线程,在创建和修改图形组件之前,同步子线程和主线程是很重要的。
  Handler类允许你向创建Handler类的线程委派一个方法。使用Handler类,你可以借用post方法来从后台线程通知更新UI。下面的例子显示了使用Handler来更新GUI线程的框架代码:

  hread:

java代码:
  1. // Initialize a handler on the main thread.
  2. private Handler handler = new Handler();

  3. private void mainProcessing() {
  4. Thread thread = new Thread(null, doBackgroundThreadProcessing, “Background”);
  5. thread.start();
  6. }
  7. private Runnable doBackgroundThreadProcessing = new Runnable() {
  8. public void run() {
  9. backgroundThreadProcessing();
  10. }
  11. };
  12. // Method which does some processing in the background.
  13. private void backgroundThreadProcessing() {
  14. [ ... Time consuming operations ... ]
  15. handler.post(doUpdateGUI);
  16. }
  17. // Runnable that executes the update GUI method.
  18. private Runnable doUpdateGUI = new Runnable() {
  19. public void run() {
  20. updateGUI();
  21. }
  22. };
  23. private void updateGUI() {
  24. [ ... Open a dialog or modify a GUI element ... ]
  25. }
复制代码

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

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

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

  下面的代码演示了如何移动EarthquakeService中的网络搜索和XML解析的操作到一个后台线程中:
  1。 重命名refreshEarthquakes方法为doRefreshEarthquakes。

java代码:
  1. private void doRefreshEarthquakes() {
  2. [ ... previous refreshEarthquakes method ... ]
  3. }
复制代码

        2. 创建一个新的refreshEarthquakes方法。它需要启动一个执行新命名的doRefreshEarthquakes方法的线程。

java代码:
  1. private void refreshEarthquakes() {
  2. Thread updateThread = new Thread(null, backgroundRefresh,“refresh_earthquake”);
  3. updateThread.start();
  4. }

  5. private Runnable backgroundRefresh = new Runnable() {
  6. public void run() {
  7. doRefreshEarthquakes();
  8. }
  9. };
复制代码


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值