• 利用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. }  

  • 相关阅读:
    .NET性能调优之三:YSlow相关规则的调优工具和方法
    .NET性能调优之二:使用Visual Studio进行代码度量
    OSPF的常见前7类LSA详解
    Packet Tracer 5.3搭建DNS服务器
    中型网络中网络冗余设计的要领
    GNS3模拟器如何保存配置文件
    CCNA相当综合的实验1
    GNS模拟器中支持的模块说明
    典型配置:H3C基于AP限制接入客户端数量
    破解H3C交换机密码的方法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318951.html
Copyright © 2020-2023  润新知