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

his isthe second part of my series of blog posts on Android non-UI thread-to-UI thread communications. Seeherefor the start of the series. As a refresher, this series of posts is about how non-UI threads can communicate back to the UI thread in an Android application in order to update the user interface. 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.

THE SIMPLE APP – REVIEW

In my last post, I provided a simple app (called Simple App)that provided two buttons to start/stop a non-UI thread. The non-UI thread’s job is to simulate long running work was 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.

simpleappdiag

In the first of this five part series, I showed you how to use an Activity’s runOnUiThread() method in a non-UI threadto post requests to change the user interface viathe UI thread’s event message queue. In this blog, option #2 is explained – using the post() method of an Android View component to request changes to the UI.

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

OPTION 2 – USING VIEW’S POST() METHOD

When a non-UI thread has access to aViewcomponent from the user interface, the non-UI thread can make a call to that View component’s post() method to request a UI change.

Just like when calling on runOnUiThread() of an Activity, calling on the View’s post()communicates the desire to request work be run on the UI thread by publishing a message in the event queue of the UI thread. When it can, the UI thread picks up the action message in the event queue and performs the UI change. So the post() method is a convenience (just as runOnUiThread is) for completing this messaging operation.

Here is the non-UI thread class – DoSomethingThread from the SimpleApp – that generates a random number and then calls on its publishProgress() method to get the random number displayed on the UI using post().

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class DoSomethingThread extends Thread { private static final String TAG = "DoSomethingThread"; private static final int DELAY = 5000; // 5 seconds private static final int RANDOM_MULTIPLIER = 10; @Override public void run() { Log.v(TAG, "doing work in Random Number Thread"); while (true) { int randNum = (int) (Math.random() * RANDOM_MULTIPLIER); publishProgress(randNum); try { Thread.sleep(DELAY); } catch (InterruptedException e) { Log.v(TAG, "Interrupting and stopping the Random Number Thread"); return; } } } private void publishProgress(int randNum) { Log.v(TAG, "reporting back from the Random Number Thread"); final String text = String.format(getString(R.string.service_msg), randNum); mainFrag.resultsTextView.post(new Runnable() { @Override public void run() { mainFrag.getResultsTextView().setText(text); } }); } }

Note in line 3 of the publishProgress() method above in the one line of code that changes between Option 1 (using runOnUiThread) and Option 2 here (using post).

CONSIDERATIONS OF OPTION 2 – POST() METHOD

As indicated, the post() method is using the same event message queue under the covers as runOnUiThread(). So in some regards, many of the pro/cons of post() are that of runOnUiThread(). A StackOverflow post (here) provides some detailed insight.

Essentially, in order to use post(), your non-UI thread must have awareness of a View component (from the UI). This allows the non-UI thread to avoid direct connection to the Activity (which was required with runOnUiThread). There are times when your non-UI thread may know a View and not an Activity or vice versa. However, both optionsmight moretightly couple the non-UI thread to the UI side given View components and Activities are UI “stuff.”

Also as with the runOnUiThread() option, this post() method option requires you to create and manage your threads more directly – and thereby have more control of the thread and its communication but also require you to have more experience with Java concurrency/thread APIs and issues.

Unlike the runOnUiThread() method, the post() method does not check whetherthe current thread is the UI thread. Therefore the post() method does not cause the Runnable to executeimmediately if it is on the UI thread. Instead, post() always has an event message pushed to the message queue for reaction by the UI thread.

WRAP UP

Stay tuned to this blog site for the next 3 options regarding the Android thread communications. Again, if you need help on your mobile project or need some mobile training(Android, iOS, other) let Intertech help. Pleasecontact us.



Read more:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-2-of-5/#ixzz3MyttDTTp
Follow us:@IntertechInc on Twitter|Intertech on Facebook

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 初次接触Android APP开发之安装Android s
  2. 在AndroidStudio中引用库 但不复制到主项
  3. Android(安卓)按钮响应点击事件的三种实
  4. Android 输入法弹出监听,保持标题固定不被
  5. 阅读《Android 从入门到精通》(4)——人机
  6. Android Developers:保存文件
  7. Android 长按home键强制关闭应用后,在哪里
  8. 三年Android技术支持工作总结及心得
  9. android的消息处理机制(Looper,Handler,M
  10. fix android build error : undefined re