• 安卓--子线程和主线程之间的交互实例(时钟)


    转自:http://blog.csdn.net/yayun0516/article/details/43115219

    .xml代码如下:

    [html] view plaincopy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <AnalogClock  
    8.         android:id="@+id/myAnalogClock"  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content"/>  
    11.   
    12.     <TextView  
    13.         android:id="@+id/info"  
    14.         android:layout_width="fill_parent"  
    15.         android:layout_height="wrap_content"  
    16.         android:text="" />  
    17.   
    18. </LinearLayout>  

    .java程序代码如下:

    [java] view plaincopy
    1. package org.lxh.demo;  
    2.   
    3. import java.util.Date;  
    4. import java.text.SimpleDateFormat;  
    5. import android.app.Activity;  
    6. import android.os.Bundle;  
    7. import android.os.Handler;  
    8. import android.os.Message;  
    9. import android.widget.TextView;  
    10.   
    11. public class Hello extends Activity {  
    12.     private TextView info = null;  
    13.     private static final int SET = 1;  
    14.     private Handler handler = new Handler() {  
    15.   
    16.         @Override  
    17.         public void handleMessage(Message msg) {  
    18.             switch (msg.what) {  
    19.             case SET:  
    20.                 Hello.this.info.setText("当前时间为:" + msg.obj.toString());  
    21.                 break;  
    22.             }  
    23.         }  
    24.   
    25.     };  
    26.   
    27.     private class ClockThread implements Runnable {//显示时间的线程类  
    28.   
    29.         public void run() {  
    30.             while (true) {  
    31.                 try {  
    32.                     Message msg = Hello.this.handler.obtainMessage(Hello.SET,  
    33.                             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")  
    34.                                     .format(new Date()));  
    35.                     Hello.this.handler.sendMessage(msg);  
    36.                     Thread.sleep(1000);  
    37.                 } catch (Exception e) {  
    38.                 }  
    39.             }  
    40.   
    41.         }  
    42.   
    43.     }  
    44.   
    45.     public void onCreate(Bundle savedInstanceState) {  
    46.         super.onCreate(savedInstanceState); // 生命周期方法  
    47.         super.setContentView(R.layout.main); // 设置要使用的布局管理器  
    48.         this.info = (TextView) super.findViewById(R.id.info);  
    49.         new Thread(new ClockThread()).start();//启动线程  
    50.   
    51.     }  
    52.   
    53. }  
    运行实例如下:


  • 相关阅读:
    Jmeterif controller 使用
    转载App测试工具大全
    Jmeter 官方在线文档&Android SDK 官方下载地址
    APP自动化之uiautomator2 +python3 UI自动化
    uiautomatorviewer不支持安卓 9.0或以上,提示:"error: obtaining UI hierachy"解决方法
    调查显示:软件开发公司出现“人才荒”
    浅谈Lean UX:我们到底该怎么设计?
    谷歌工程师再次公布Windows漏洞 并称微软很难合作
    灵活运用AppFlood:提高APP eCPM的10个技巧
    Spring Framework 4.0M1发布,支持JDK 8、Java EE 7
  • 原文地址:https://www.cnblogs.com/walccott/p/4957017.html
Copyright © 2020-2023  润新知