今天在工作的时候,遇到了一个方法,是关于Handle来实现延时操作的,自己写了一个小demo,学习总结如下

xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <RelativeLayout
  9. android:id="@+id/abc"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:orientation="vertical"
  13. android:padding="30dp" >
  14.  
  15. <RelativeLayout
  16. android:id="@+id/ll"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:padding="20dp" >
  20.  
  21. <TextView
  22. android:id="@+id/tv_time"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:background="@drawable/select"
  26. android:text="time"
  27. android:textColor="#ffffff"
  28. android:textSize="30dp" />
  29.  
  30. <TextView
  31. android:id="@+id/tv_click"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_alignParentRight="true"
  35. android:background="@drawable/select"
  36. android:text="click"
  37. android:textColor="#ffffff"
  38. android:textSize="30dp" />
  39. </RelativeLayout>
  40.  
  41. <TextView
  42. android:id="@+id/tv_run"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@+id/ll"
  46. android:layout_centerHorizontal="true"
  47. android:background="@drawable/select"
  48. android:padding="20dp"
  49. android:text="wait...."
  50. android:textColor="#ffffff"
  51. android:textSize="30dp" />
  52.  
  53. <Button
  54. android:id="@+id/bt1"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_below="@+id/tv_run"
  58. android:src="@drawable/select"
  59. android:text="start"
  60. android:textColor="#ff0000" />
  61.  
  62. <Button
  63. android:id="@+id/bt2"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:layout_below="@+id/tv_run"
  67. android:layout_centerHorizontal="true"
  68. android:src="@drawable/select"
  69. android:text="stop"
  70. android:textColor="#ff0000" />
  71.  
  72. <Button
  73. android:id="@+id/bt3"
  74. android:layout_width="wrap_content"
  75. android:layout_height="wrap_content"
  76. android:layout_alignParentRight="true"
  77. android:layout_below="@+id/tv_run"
  78. android:src="@drawable/select"
  79. android:text="clear"
  80. android:textColor="#ff0000" />
  81. </RelativeLayout>
  82.  
  83. </LinearLayout>

java如下

  1. package com.example.alert;
  2.  
  3. import android.R.integer;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9.  
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15.  
  16. public class HeadSetActivity extends Activity {
  17. private TextView tv_time;
  18. private TextView tv_click;
  19. private TextView tv_run;
  20. private Button button1;
  21. private Button button2;
  22. private Button button3;
  23. private int time = 0;
  24. private boolean time_begin = true;
  25. private int click = 0;
  26. //创建消息队列
  27. private Handler uiHandle = new Handler(){
  28. public void handleMessage(Message msg) {
  29. switch(msg.what){
  30. //实现计时器功能
  31. case 1:
  32. time++;
  33. tv_time.setText("time:"+time);
  34. uiHandle.sendEmptyMessageDelayed(1, 1000);
  35. break;
  36. default: break;
  37. }
  38. }
  39. };
  40. //开启一个线程
  41. private Runnable runnable = new Runnable() {
  42.  
  43. @Override
  44. public void run() {
  45. // TODO Auto-generated method stub
  46. tv_run.setText("runnable is start|time="+time);
  47. }
  48. };
  49. @Override
  50. public void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52. setContentView(R.layout.activity_main);
  53. tv_time = (TextView)findViewById(R.id.tv_time);
  54. tv_click = (TextView)findViewById(R.id.tv_click);
  55. tv_run = (TextView)findViewById(R.id.tv_run);
  56. button1 = (Button) findViewById(R.id.bt1);
  57. button2 = (Button) findViewById(R.id.bt2);
  58. button3 = (Button) findViewById(R.id.bt3);
  59. //启动计时器以及runnable延时操作
  60. button1.setOnClickListener(new OnClickListener() {
  61.  
  62. @Override
  63. public void onClick(View arg0) {
  64. // TODO Auto-generated method stub
  65. //启动activity1
  66. if (time_begin) {
  67. uiHandle.sendEmptyMessageDelayed(1, 1000);
  68. tv_run.setText("runnable wait begin|time="+time);
  69. time_begin = false;
  70. }
  71. click++;
  72. tv_click.setText("click:"+click);
  73.  
  74. uiHandle.postDelayed(runnable,4000);
  75. }
  76.  
  77. });
  78. //移除runnable延时操作
  79. button2.setOnClickListener(new OnClickListener() {
  80.  
  81. @Override
  82. public void onClick(View arg0) {
  83. // TODO Auto-generated method stub
  84. uiHandle.removeCallbacks(runnable);
  85. tv_run.setText("runnable is clear|time ="+time);
  86.  
  87. }
  88. });
  89. //清除消息队列的所有操作
  90. button3.setOnClickListener(new OnClickListener() {
  91.  
  92. @Override
  93. public void onClick(View arg0) {
  94. // TODO Auto-generated method stub
  95. uiHandle.removeMessages(1);
  96. tv_click.setText("click");
  97. tv_run.setText("wait...");
  98. tv_time.setText("time");
  99. uiHandle.removeCallbacks(runnable);
  100. time_begin = true;
  101. time = 0;
  102. click = 0;
  103. }
  104. });
  105. }
  106.  
  107. }

我们看下如下的演示结果

从上面的演示结果我们可以得出关于postDelayed 的知识点总结

1.这个方法是用来延时执行一个动作的

2.多次执行这个动作,并不会移除之前仍然处于延时等待状态的动作

3.如果想要结束处于等待执行中的动作,我们可以主动调用

  1. uiHandle.removeCallbacks(runnable);

Handle-postDelayed 延迟操作的更多相关文章

  1. for循环+setTimeout的延迟操作

    例子: for (var i = 0; i < 5; i++) { setTimeout(function () { console.log(i); }, 100) } 上述代码,输出结果显而易 ...

  2. [从源码学设计]蚂蚁金服SOFARegistry之延迟操作

    [从源码学设计]蚂蚁金服SOFARegistry之延迟操作 0x00 摘要 SOFARegistry 是蚂蚁金服开源的一个生产级.高时效.高可用的服务注册中心. 本系列文章重点在于分析设计和架构,即利 ...

  3. 数据结构--线段树--lazy延迟操作

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 53749   ...

  4. new Handler().postDelayed() 延迟intent跳转

    原文地址http://blog.csdn.net/x605940745/article/details/19401549 new Handler().postDelayed(new Runnable( ...

  5. iOS 中 延迟操作四种方式

    本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...

  6. Java_延迟操作

    1. 用Thread就不会iu无法终止     new Thread(new Runnable() {               public void run() {                ...

  7. Kafka技术内幕 读书笔记之(五) 协调者——延迟的加入组操作

      协调者处理不同消费者的“加入组请求”,由于不能立即返回“加入组响应”给每个消费者,它会创建一个“延迟操作”,表示协调者会延迟发送“加入组响应”给消费者 . 但协调者不会为每个消费者的 “加入组请求 ...

  8. 【Win 10应用开发】延迟共享

    延迟共享是啥呢,这么说吧,就是在应用程序打开共享面板选择共享目标时,不会设置要共享的数据,而是等到共享目标请求数据时,才会发送数据,而且,延迟操作可以在后台进行. 这样说似乎过于抽象,最好的诠释方法, ...

  9. as3延迟处理

    查找关键字“flashplayer 弹性跑道” 每当一帧即将走完,FlashPlayer就要做些总结性工作(一次性地汇总变化),把这一帧当中发生的变化拿出来展示(渲染)一下. 如果它处理的事情少,工作 ...

随机推荐

  1. rest_framework(解析器 上)

    rest_framework 解析器 对请求题数据进行解析 url from django.conf.urls import url,include from cmdb import views ur ...

  2. Mvc前后端显示不同的404错误页

    最近做的系统前端是移动端的,后端是PC端,然后404页面不能用通一个,so  查找了一些资料,找到了一个解决办法 在Global.asax文件夹下添加Application_EndRequest事件处 ...

  3. Android 两步搞定Fragment的返回键

    Fragment可以说是在Android开发必需要使用到技术,项目中的界面基本上都是使用Fragment来实现,而Activity只是作为Fragment的载体,但有些特殊情况下Fragment也不得 ...

  4. Android开发日志统一管理

    在开发中,我们通常要对日志的输出做统一管理,下面就为大家推荐一个日志输出类,在开发阶段只需将DEBUG常量设为true,生产环境将DEBUG设为false即可控制日志的输出.啥都不说了,需要的朋友直接 ...

  5. Objective-C 小记(10)__weak

    本文使用的 runtime 版本为 objc4-706. __weak 修饰的指针最重要的特性是其指向的对象销毁后,会自动置为 nil,这个特性的实现完全是依靠运行时的.实现思路是非常简单的,对于下面 ...

  6. 最大优先队列 A - 奇怪的玩意

    我们的化学生物学家发明了一种新的叫stripies非常神奇的生命.该stripies是透明的无定形变形虫似的生物,生活在果冻状的营养培养基平板菌落.大部分的时间stripies在移动.当他们两个碰撞, ...

  7. highGUI图形用户界面

    #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> using namespace ...

  8. 洛谷3833 [SHOI2012]魔法树

    SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点 ...

  9. cmd 操作命令

    1)cd 操作文件目录的 cd path #进入path cd / #返回到当前盘符的根目录 cd .. #返回到上级目录 2)dir 显示当前目录 dir #显示当前目录下的文件夹 dir path ...

  10. 题解 P1198 【[JSOI2008]最大数】

    说起来这还是蒟蒻AC的第一道省选线段树呢. 这道题和其他线段树最大的不同就是在于本题数组一直在增大. 寻常的线段树蒟蒻习惯用如下的结构体储存,然而对于此题就不行了: struct node{ int ...