When our application can not respond the user input in time, the Android will invoke a ANR dialog (shown as the following figure)
As we know, if we do nothing,all Android application components — including Activities, Services, and Broadcast Receivers — start on the main application thread. So, if our application invoke some action that needs
a long time, we'd better put it into another thread. In this way, our main thread can respond user action in time.
One of the functionality of Android Handler isto enqueue an action to be performed on a different thread than your own. So, we can put our i/o
operation into a Handler. The following steps show how we can achieve this.
Step 0: Perpare a Runnable object who will do some processing in background:
Runnable worker = new Runnable()
{
@Override
public void run()
{
Toast.makeText(HandlerDemo.this,
"threading running", Toast.LENGTH_SHORT)
.show();
}
};
Step 1: Construct a Hanlder object in main thread.
/**
* Default constructor associates this handler with the queue for
* the current thread. If there isn't one, this handler won't be
* able to receive messages.
*/
handler = new Handler();
Step 2: Add the Runnable object to the messge queue:
/**
* Causes the Runnable r to be added to the message queue. The
* runnable will be run on the thread to which this handler is
* attached.
*/
handler.post(worker);
Ok, by now, the Runnable object will do the precess in a background thread for you. In this way, your main thread can respond the UI events now.
分享到:
相关推荐
通过 Foxit PDF Preview Handler 可以让你的OutLook2007预览PDF 文件。 Foxit PDF Preview Handler 1.1 For XP
handler发送和接收message的各种方式 (1)Handler.SendEmptyMessage(what); (2)Handler.obtainMessage(int what, int arg1, int arg2, Object obj).sendYToTarget(); (3)Message.obtain(handler, MESSAGE_MSG, 0, 0, ...
整理了Handler的各种使用方法
Interactive Applications Using Matplotlib will teach you how to turn your plots into fully interactive applications for data exploration and information synthesis. After being introduced to the ...
android 中Handler 的几种写法,很简单的demo,大神简单修改下,用的是Handler.Callback,的方法
Android Handler类详解 Android Handler类详解 Android Handler类详解 Android Handler类详解
Handler 推荐用法 demo
android:handler简单用法
赠送jar包:netty-handler-4.1.73.Final.jar; 赠送原API文档:netty-handler-4.1.73.Final-javadoc.jar; 赠送源代码:netty-handler-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-handler-4.1.73....
demo01: 通过handler.post()的方法可以更新ui demo02: 一秒钟切换一张图片 demo03: 霓虹灯动态效果的显示 demo04: 倒计时,停止倒计时 demo05: 通过handler.sendMessage和handleMessage发送处理消息 demo06: message...
1.handler是什么? 2.handler怎么用? 3.为什么要用handler?
资源包括如何在一个activity A里激活另一个activity B。 然后在两个activity之间用handler传输数据。在activity A里定义全局变量handler,用sendMessage来发数据,在handler里数据以消息队列的方式发送。
感谢慕课网,浅谈handler
handler的简单示例,通过异步更新ui
这是我用java代码实现的Handler
Android线程操作 handler demo
asp.net mvc handler
Android Handler传值的,简单的介绍了Handler的用法
Handler handler与Action相关联,一个Action可以有多个Component引用; Action是一个可被共享的对象,有五个主要的属性:text, handler, iconCls, disabled, hidden component的构建方式比较有意思: new Ext...
Handler,Looper和Message的理解和练习