Android开发之Handler
我们都知道应用程序开启后,安卓会开启一个主线程(UI线程),主线程管理UI控件,进行事件分发。那为什么会出现Handler呢?
例如你要是点击一个 Button ,Android会分发事件到Button上,来响应你的操作。 如果此时需要一个耗时的操作,例如: 联网读取数据, 或者读取本地较大的一个文件的时候,你把这些操作放在主线程中, 如果5秒钟还没有完成的话,界面会出现假死现象,会收到Android系统的一个错误提示 "强制关闭"。 这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的, 也就是说,更新UI只能在主线程中更新,子线程中操作是危险的。 这个时候,安卓开发为了解决这个问题Handler就出现了。
handler可以分发Message对象和Runnable对象到主线程中, 每个Handler实例,都会绑定到创建他的线程中(一般是位于主线程),它有两个作用:
(1)安排消息或Runnable 在某个主线程中某个地方执行;
(2)安排一个动作在不同的线程中执行。
Handler分发消息的一些方法:
post(Runnable)
postAtTime(Runnable,long)
postDelayed(Runnable long)
sendEmptyMessage(int)
sendMessage(Message)
sendMessageAtTime(Message,long)
sendMessageDelayed(Message,long)
post类方法允许你排列一个Runnable对象到主线程队列中,
sendMessage类方法, 允许你安排一个带数据的Message对象到队列中,等待更新。
下面是一个简单的实例:
package com.example.nihongdeng;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView; public class FrameLayoutTest extends Activity { private int currentColor=0;
final int []colors=new int[]
{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6
};
final int []names=new int[]
{
R.id.view01,
R.id.view02,
R.id.view03,
R.id.view04,
R.id.view05,
R.id.view06
};
TextView []views=new TextView[names.length];
Handler handler=new Handler()
{
public void handleMessage(Message msg)//要接收数据必须重写此方法
{
if (msg.what==0x123)//接收到数据所进行的操作
{
for (int i=0;i<names.length;i++)
{
views[i].setBackgroundResource(colors[(i+currentColor)%colors.length]);
}
currentColor++;
}
super.handleMessage(msg);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for (int i=0;i<names.length;i++)
{
views[i]=(TextView)findViewById(names[i]);
}
new Timer().schedule(
new TimerTask()
{ @Override
public void run() {
// TODO Auto-generated method stub
handler.sendEmptyMessage(0x123);
} },0,200);
}
}
这个例子的效果是实现一种霓虹灯的效果:
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">"
<TextView
android:id="@+id/view01"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="320px"
android:height="320px"
android:background="@color/color1"/>
<TextView
android:id="@+id/view02"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="280px"
android:height="280px"
android:background="@color/color2"/>
<TextView
android:id="@+id/view03"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="240px"
android:height="240px"
android:background="@color/color3"/>
<TextView
android:id="@+id/view04"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200px"
android:height="200px"
android:background="@color/color4"/>
<TextView
android:id="@+id/view05"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="160px"
android:height="160px"
android:background="@color/color5"/>
<TextView
android:id="@+id/view06"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
android:height="120px"
android:background="@color/color6"/> </FrameLayout>
代码我们通过Timer类来实现一个线程:
Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。它可以计划执行一个任务一次或反复多次。
TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务。
schedule的意思(时间表、进度表)
timer.schedule(task,1000, 1000); //task是所要执行的任务,延时1000ms后执行,1000ms执行一次。
以上案例则是不延时执行,每0.2秒发送一次消息,接收到消息后对UI界面进行更新。
Android开发之Handler的更多相关文章
- Android开发之Handler的用法(源码分享)
Handler主要接受子线程发送的数据, 并用此数据配合主线程更新UI.. 当应用程序启动时.Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发. ...
- Android开发之Handler和Looper的关系
关于Handler的总结. Message:消息,当中包括了消息ID,消息处理对象以及处理的数据等,由MessageQueue统一列队,终由Handler处理. Handler:处 ...
- Android开发之bindService()侦听service内部状态
在Android开发之bindService()通信的基础上,实现bindService()方法侦听service内部状态. 实现侦听service内部状态,使用的是回调机制 1.首先实现一个接口 p ...
- Android开发之Java集合类性能分析
对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之Git配置
Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
随机推荐
- XML常用颜色值
<?xml version="1.0" encoding="utf-8" ?> <resources> <color name=& ...
- Linux入门1
在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe.它接收用户命令,然后调用相应的应用程序. Li ...
- linux的nohup命令的用法
在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/mysql/bin/my ...
- linux获取目录下文件
查看当前目录下的文件: find . -type f 查看当前目录下的文件夹: find . -type d 如果文件file1不为空: if [ -s file1 ];then echo ...
- 如何通过Visual Studio发布Azure应用程序
发布 Azure 云服务 使用 Azure Tools for Visual Studio,可以直接从 Visual Studio 将云服务发布到 Azure. 在发布 Azure 云服务之前,必须已 ...
- HDOJ-ACM1097(JAVA) A hard puzzle
这道题就是HDOJ的1061的变形: 1061 :求n的n次方的个位数 http://www.cnblogs.com/xiezie/p/5596779.html 1097 :求n的m次方的个位数 因此 ...
- hdoj 1406 完数
完数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- DevExpress MessageBox 弹出框 底层类
效果图: 前台调用: //图一的前台调用 MessageBox.Show("测试", "标题", MessageBoxButtons.OK); //图二的前台调 ...
- LIMS系统供应商一览表
LIMS系统供应商一览表. 国内自主研发的LIMS供应商的产品质量一般,国外的LIMS产品在本土化方面,北京三维天地的质量最佳. LIMS系统JAVA..Net平台上都有,由于实验室业务数据量等原因, ...
- navicate恢复数据
恢复psc文件,出现记录数为0 不要使用事务,选用遇到错误继续