Handle-postDelayed 延迟操作
今天在工作的时候,遇到了一个方法,是关于Handle来实现延时操作的,自己写了一个小demo,学习总结如下
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" > <RelativeLayout
android:id="@+id/abc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="30dp" > <RelativeLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp" > <TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select"
android:text="time"
android:textColor="#ffffff"
android:textSize="30dp" /> <TextView
android:id="@+id/tv_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/select"
android:text="click"
android:textColor="#ffffff"
android:textSize="30dp" />
</RelativeLayout> <TextView
android:id="@+id/tv_run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll"
android:layout_centerHorizontal="true"
android:background="@drawable/select"
android:padding="20dp"
android:text="wait...."
android:textColor="#ffffff"
android:textSize="30dp" /> <Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_run"
android:src="@drawable/select"
android:text="start"
android:textColor="#ff0000" /> <Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_run"
android:layout_centerHorizontal="true"
android:src="@drawable/select"
android:text="stop"
android:textColor="#ff0000" /> <Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv_run"
android:src="@drawable/select"
android:text="clear"
android:textColor="#ff0000" />
</RelativeLayout> </LinearLayout>
java如下
package com.example.alert; import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message; import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class HeadSetActivity extends Activity {
private TextView tv_time;
private TextView tv_click;
private TextView tv_run;
private Button button1;
private Button button2;
private Button button3;
private int time = 0;
private boolean time_begin = true;
private int click = 0;
//创建消息队列
private Handler uiHandle = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what){
//实现计时器功能
case 1:
time++;
tv_time.setText("time:"+time);
uiHandle.sendEmptyMessageDelayed(1, 1000);
break;
default: break;
}
}
};
//开启一个线程
private Runnable runnable = new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
tv_run.setText("runnable is start|time="+time);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_time = (TextView)findViewById(R.id.tv_time);
tv_click = (TextView)findViewById(R.id.tv_click);
tv_run = (TextView)findViewById(R.id.tv_run);
button1 = (Button) findViewById(R.id.bt1);
button2 = (Button) findViewById(R.id.bt2);
button3 = (Button) findViewById(R.id.bt3);
//启动计时器以及runnable延时操作
button1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//启动activity1
if (time_begin) {
uiHandle.sendEmptyMessageDelayed(1, 1000);
tv_run.setText("runnable wait begin|time="+time);
time_begin = false;
}
click++;
tv_click.setText("click:"+click); uiHandle.postDelayed(runnable,4000);
} });
//移除runnable延时操作
button2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
uiHandle.removeCallbacks(runnable);
tv_run.setText("runnable is clear|time ="+time); }
});
//清除消息队列的所有操作
button3.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
uiHandle.removeMessages(1);
tv_click.setText("click");
tv_run.setText("wait...");
tv_time.setText("time");
uiHandle.removeCallbacks(runnable);
time_begin = true;
time = 0;
click = 0;
}
});
} }
我们看下如下的演示结果
从上面的演示结果我们可以得出关于postDelayed 的知识点总结
1.这个方法是用来延时执行一个动作的
2.多次执行这个动作,并不会移除之前仍然处于延时等待状态的动作
3.如果想要结束处于等待执行中的动作,我们可以主动调用
uiHandle.removeCallbacks(runnable);
Handle-postDelayed 延迟操作的更多相关文章
- for循环+setTimeout的延迟操作
例子: for (var i = 0; i < 5; i++) { setTimeout(function () { console.log(i); }, 100) } 上述代码,输出结果显而易 ...
- [从源码学设计]蚂蚁金服SOFARegistry之延迟操作
[从源码学设计]蚂蚁金服SOFARegistry之延迟操作 0x00 摘要 SOFARegistry 是蚂蚁金服开源的一个生产级.高时效.高可用的服务注册中心. 本系列文章重点在于分析设计和架构,即利 ...
- 数据结构--线段树--lazy延迟操作
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53749 ...
- new Handler().postDelayed() 延迟intent跳转
原文地址http://blog.csdn.net/x605940745/article/details/19401549 new Handler().postDelayed(new Runnable( ...
- iOS 中 延迟操作四种方式
本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...
- Java_延迟操作
1. 用Thread就不会iu无法终止 new Thread(new Runnable() { public void run() { ...
- Kafka技术内幕 读书笔记之(五) 协调者——延迟的加入组操作
协调者处理不同消费者的“加入组请求”,由于不能立即返回“加入组响应”给每个消费者,它会创建一个“延迟操作”,表示协调者会延迟发送“加入组响应”给消费者 . 但协调者不会为每个消费者的 “加入组请求 ...
- 【Win 10应用开发】延迟共享
延迟共享是啥呢,这么说吧,就是在应用程序打开共享面板选择共享目标时,不会设置要共享的数据,而是等到共享目标请求数据时,才会发送数据,而且,延迟操作可以在后台进行. 这样说似乎过于抽象,最好的诠释方法, ...
- as3延迟处理
查找关键字“flashplayer 弹性跑道” 每当一帧即将走完,FlashPlayer就要做些总结性工作(一次性地汇总变化),把这一帧当中发生的变化拿出来展示(渲染)一下. 如果它处理的事情少,工作 ...
随机推荐
- django 笔记11 装饰器
在views.py创建 一般用来cookies的装饰器 def auth(func): def inner(request, *args, **kwargs): v = request.COOKIES ...
- springboot 注入xml自定义类
新建入口类可扫描类: @Configuration @ImportResource(locations = {"classpath:spring-bean.xml"}) publi ...
- Sequences of sequences
I have focused on lists of tuples, but almost all the examples in this chapter also work with lists ...
- Traversal with a for loop
A lot of computations involve processing a string one character at a time. Often they start at the b ...
- HTML5,CSS3新特性,与旧版的区别
HTML5新特性 (1)语意化更好的内容元素,比如 article.footer.header.nav.section (2)本地存储.sessionStorage.localStorage和inde ...
- AngularJs轻松入门(二)数据绑定
数据绑定是AngularJs中非常重要的特性,我们看一下下面的例子: <!DOCTYPE html> <html ng-app> <head lang="en& ...
- Android属性动画-ValueAnimator和ObjectAnimator的高级用法
ValueAnimator的高级用法 在上篇文章中介绍补间动画缺点的时候有提到过,补间动画是只能对View对象进行动画操作的.而属性动画就不再受这个限制,它可以对任意对象进行动画操作.那么大家应该还记 ...
- vue 实现 点击取消监控内容是否发生修改 若修改提示 是否需要保存
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- grvphviz && dot脚本语言
安装graphviz 可去官网下载http://www.graphviz.org/download/下载之后按步骤安装 打开编辑器,创建*.dot文件,编辑dot脚本代码,保存. D:\>dot ...
- [TJOI2013]单词 AC 自动机
题目描述: 小张最近在忙毕设,所以一直在读论文. 一篇论文是由许多单词组成但小张发现一个单词会在论文中出现很多次,他想知道每个单词分别在论文中出现了多少次. 题解: AC 自动机裸题,将所有字符串读入 ...