博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Non-UI to UI Thread Communications(Part 3 of 5)
阅读量:5220 次
发布时间:2019-06-14

本文共 5554 字,大约阅读时间需要 18 分钟。

Original:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-3-of-5/

Continuing my series on Android non-UI thread-to-UI thread communications, this post covers use of the Handler Framework to facilitate the communications.  See here for  and  of the series.

Non-UI threads are not allowed to make updates to the UI.  Trying to do too much work (as defined as not allowing the user to interact with the UI for more than 5 seconds) on the UI thread leads to ANR errors.  In the first two posts, I showed how to use an activity’s runOnUiThread() method and a view component’s post() method to have the non-UI thread send a request through the underlying UI event message channel to the UI thread to execute a UI update.

ANDROID’S HANDLER FRAMEWORK

Android threads, in particular the UI thread, have a message queue.  Messages in the queue are processed by the thread in order of arrival.  In the case of the UI thread, user events (like a button push) cause event messages to be placed in the queue.  As explained in the previous posts, the runOnUiThread() and post() methods use this queue under the covers.  However, you can use the message queue more directly.

Using the , you can create a message directly and put the message on the UI thread’s queue from the non-UI thread.  The framework also lets you build a message handler to listen for the message on the UI thread.  Thus, the Handler Framework can provide another means for the non-UI thread to communicate with the UI via the framework pieces.

THE SIMPLEAPP REVIEW

As with the last post, I provide the simple application (called Simple App) to demonstrate the use of the Handler Framework for thread communications.  The app has two buttons to start/stop a non-UI thread.  The non-UI thread’s job is to simulate long running work by generating a random number, call the UI to have a TextView widget update the display of the random number, and then sleep for a number of seconds.

Now let’s see how the Handler Framework can be used in this app to update the UI (the TextView) from the non-UI thread.

OPTION 3 – USING THE HANDLER FRAMEWORK

First, create a Handler in the UI thread to receive and react to new messages sent by the non-UI thread.  Here are the steps to create the Handler:

1. Create class that extends . For simplicity, I created the Handler subclass (called HandlerExtension here) as an inner class to the application’s activity.

The code here may look a little complex due to the static nature and WeakReference in the subclass.  If you try to create a simple class that extends Handler, you will find Eclipse and the SDK issues a compiler warning that the Handler class should be static or leaks might occur.

The issue is well explained in a .  While the code may be a little more complex, the post provides a great template example for creating the Handler subclass without memory leaks.

2. Next, add a property to hold the Handler subclass instance in the Activity.

3. Then, from the activity’s onCreate() method, create an instance of the Handler so that it can start processing any incoming messages from the non-UI thread.

Second, with the Handler subclass code in place, you can create a message from the non-UI thread and publish it into the UI thread’s message queue using the Handler subclass. Simply create an instance of and add data to the message to indicate to the Handler what UI changes should occur (in this case, providing the random number that needs to be displayed in the TextView widget).

The SimpleApp example code for demonstrating option #3 can be found (in an Eclipse project ZIP file).

CONSIDERATIONS OF OPTION 3 – HANDLER FRAMEWORK

The runOnUiThread() and post() methods examined in previous posts are really special Hander Framework conveniences.  They use the event queue on the UI thread to perform their task.  So why use the Handler Framework directly as shown here?  Using the Handler Framework directly is a bit more complex, but it allows you more control.  This is a generic framework for thread communication – any thread.  It also allows the non-UI thread to communicate without direct knowledge/ties to the activity or UI side components.  The non-UI merely has to post a message to a handler.

WRAP UP

In the final two upcoming posts of this series you will see different Android infrastructure to perform the non-UI to UI thread communications – namely the use of Broadcast Receivers and AsyncTask.  Stay tune for those posts.  If you are looking for some Android training or consulting help, look no further than the sponsor of this blog site:  .

 

Read more:  
Follow us:  | 

转载于:https://www.cnblogs.com/GoAhead/p/4186663.html

你可能感兴趣的文章
算数几何均值不等式,柯西不等式,琴生Jensen不等式
查看>>
mysql group by的用法 注意
查看>>
IList和DataSet性能差别 转自 http://blog.csdn.net/ilovemsdn/article/details/2954335
查看>>
Python中的join()函数的用法
查看>>
Hive教程(1)
查看>>
黑马程序员-指针的初步认识
查看>>
提示未授予用户在此计算机上的请求登录类型
查看>>
Java集合框架学习
查看>>
第16周总结
查看>>
将Cent0S 7的网卡名称eno33改为eth0
查看>>
透明度Opacity多浏览器兼容处理
查看>>
oracle 常用简单命令语句
查看>>
【机器学习_3】常见术语区别
查看>>
Oracle基础 数据库备份和恢复
查看>>
C#编程时应注意的性能处理
查看>>
Java集合--概述
查看>>
1-TwoSum(简单)
查看>>
css box模型content-box 和border-box
查看>>
Fragment
查看>>
比较安全的获取站点更目录
查看>>