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就要做些总结性工作(一次性地汇总变化),把这一帧当中发生的变化拿出来展示(渲染)一下. 如果它处理的事情少,工作 ...
随机推荐
- rest_framework(解析器 上)
rest_framework 解析器 对请求题数据进行解析 url from django.conf.urls import url,include from cmdb import views ur ...
- Mvc前后端显示不同的404错误页
最近做的系统前端是移动端的,后端是PC端,然后404页面不能用通一个,so 查找了一些资料,找到了一个解决办法 在Global.asax文件夹下添加Application_EndRequest事件处 ...
- Android 两步搞定Fragment的返回键
Fragment可以说是在Android开发必需要使用到技术,项目中的界面基本上都是使用Fragment来实现,而Activity只是作为Fragment的载体,但有些特殊情况下Fragment也不得 ...
- Android开发日志统一管理
在开发中,我们通常要对日志的输出做统一管理,下面就为大家推荐一个日志输出类,在开发阶段只需将DEBUG常量设为true,生产环境将DEBUG设为false即可控制日志的输出.啥都不说了,需要的朋友直接 ...
- Objective-C 小记(10)__weak
本文使用的 runtime 版本为 objc4-706. __weak 修饰的指针最重要的特性是其指向的对象销毁后,会自动置为 nil,这个特性的实现完全是依靠运行时的.实现思路是非常简单的,对于下面 ...
- 最大优先队列 A - 奇怪的玩意
我们的化学生物学家发明了一种新的叫stripies非常神奇的生命.该stripies是透明的无定形变形虫似的生物,生活在果冻状的营养培养基平板菌落.大部分的时间stripies在移动.当他们两个碰撞, ...
- highGUI图形用户界面
#include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> using namespace ...
- 洛谷3833 [SHOI2012]魔法树
SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点 ...
- cmd 操作命令
1)cd 操作文件目录的 cd path #进入path cd / #返回到当前盘符的根目录 cd .. #返回到上级目录 2)dir 显示当前目录 dir #显示当前目录下的文件夹 dir path ...
- 题解 P1198 【[JSOI2008]最大数】
说起来这还是蒟蒻AC的第一道省选线段树呢. 这道题和其他线段树最大的不同就是在于本题数组一直在增大. 寻常的线段树蒟蒻习惯用如下的结构体储存,然而对于此题就不行了: struct node{ int ...