• 利用Handler定时更新Android UI


    在 Android 里定时更新 UI,通常使用的是 java.util.Timerjava.util.TimerTaskandroid.os.Handler 组合,这里有相关的讨论。但实际上 Handler 自身已经提供了定时的功能。 

    参考 android.os.Handler 的文档 
    引用

    There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own. 


    Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler). 


    When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior. 


    下面是一个简单的计数器程序,每隔一秒递增计数器 

     

    代码 

    main.xml 
    -------- 
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical" android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent">  
    5.     <TextView android:id="@+id/counter" android:layout_width="fill_parent"  
    6.         android:layout_height="wrap_content" android:text="Count: 0" />  
    7.     <LinearLayout android:orientation="horizontal"  
    8.         android:layout_width="fill_parent" android:layout_height="wrap_content">  
    9.         <Button android:text="start" android:id="@+id/Button01"  
    10.             android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0"></Button>  
    11.         <Button android:text="stop" android:id="@+id/Button02"  
    12.             android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:enabled="false"></Button>  
    13.         <Button android:text="reset" android:id="@+id/Button03"  
    14.             android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0"></Button>  
    15.     </LinearLayout>  
    16. </LinearLayout>  


    TestTimer.java 
    -------------- 
    Java代码  收藏代码
    1. package cn.yo2.aquarium.android.testtimer;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.os.Handler;  
    6. import android.view.View;  
    7. import android.view.View.OnClickListener;  
    8. import android.widget.Button;  
    9. import android.widget.TextView;  
    10.   
    11. public class TestTimer extends Activity {  
    12.     private Button btnStart;  
    13.     private Button btnStop;  
    14.     private Button btnReset;  
    15.     private TextView tvCounter;  
    16.     private long count = 0;  
    17.     private boolean run = false;  
    18.   
    19.     private Handler handler = new Handler();  
    20.   
    21.     private Runnable task = new Runnable() {  
    22.   
    23.         public void run() {  
    24.             // TODO Auto-generated method stub  
    25.             if (run) {  
    26.                 handler.postDelayed(this1000);  
    27.                 count++;  
    28.             }  
    29.             tvCounter.setText("Count: " + count);  
    30.         }  
    31.     };  
    32.   
    33.     /** Called when the activity is first created. */  
    34.     @Override  
    35.     public void onCreate(Bundle savedInstanceState) {  
    36.         super.onCreate(savedInstanceState);  
    37.         setContentView(R.layout.main);  
    38.   
    39.         btnStart = (Button) findViewById(R.id.Button01);  
    40.         btnStop = (Button) findViewById(R.id.Button02);  
    41.         btnReset = (Button) findViewById(R.id.Button03);  
    42.         tvCounter = (TextView) findViewById(R.id.counter);  
    43.   
    44.         btnStart.setOnClickListener(new OnClickListener() {  
    45.   
    46.             public void onClick(View v) {  
    47.                 // TODO Auto-generated method stub  
    48.                 run = true;  
    49.                 updateButton();  
    50.                 handler.postDelayed(task, 1000);  
    51.             }  
    52.         });  
    53.   
    54.         btnStop.setOnClickListener(new OnClickListener() {  
    55.   
    56.             public void onClick(View v) {  
    57.                 // TODO Auto-generated method stub  
    58.                 run = false;  
    59.                 updateButton();  
    60.                 handler.post(task);  
    61.             }  
    62.         });  
    63.   
    64.         btnReset.setOnClickListener(new OnClickListener() {  
    65.   
    66.             public void onClick(View v) {  
    67.                 // TODO Auto-generated method stub  
    68.                 count = 0;  
    69.                 run = false;  
    70.                 updateButton();  
    71.                 handler.post(task);  
    72.             }  
    73.         });  
    74.     }  
    75.   
    76.     private void updateButton() {  
    77.         btnStart.setEnabled(!run);  
    78.         btnStop.setEnabled(run);  
    79.     }  
    80. }  

  • 相关阅读:
    单点登陆
    【springmvc学习】常用注解总结
    Excel里函数中的万金油,你确定不要点进来看看?
    Maven3在Eclipse上安装插件
    VSCode 云同步扩展设置 Settings Sync 插件
    Ubuntu 18.04 root 使用ssh密钥远程登陆
    [Asp.net] C# 操作Excel的几种方式 优缺点比较
    开源框架Autofac使用入门
    C# .net Ueditor实现图片上传到阿里云OSS 对象存储
    [python]TypeError: string indices must be integers解决方法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318951.html
Copyright © 2020-2023  润新知