很长时间没有写博客了,发现自己慢慢的便懒了,不想动,一个呢,是天气比较热,昨天还是39.5°呢,二来最近比较忙,今天有点闲暇时间,就把以前忘记的知识都整理出来,以便以后查阅,好了,说正题吧,我们知道,用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间。
我们开启一个线程,线程每隔一秒发送一次消息,我们在消息中更新TextView上显示的时间就ok了。
首先我们在布局文件中放一个TextView用来显示时间,如下所示:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:background="@android:color/white">
7 <TextView
8 android:id="@+id/mytime"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 android:gravity="center"
12 android:textColor="@android:color/black"
13 android:textSize="36sp"/>
14 </LinearLayout>
之后我们写一个线程,线程里面无限循环,每隔一秒发送一个消息,其中由Handler来处理显示的结果:
1 public class TimeThread extends Thread {
2 @Override
3 public void run () {
4 do {
5 try {
6 Thread.sleep(1000);
7 Message msg = new Message();
8 msg.what = msgKey1;
9 mHandler.sendMessage(msg);
10 }
11 catch (InterruptedException e) {
12 e.printStackTrace();
13 }
14 } while(true);
15 }
16 }
之后会向Handler中发消息,我们可以通过Handler来更新TextView的显示内容:
1 private Handler mHandler = new Handler() {
2 @Override
3 public void handleMessage (Message msg) {
4 super.handleMessage(msg);
5 switch (msg.what) {
6 case msgKey1:
7 long sysTime = System.currentTimeMillis();
8 CharSequence sysTimeStr = DateFormat.format("hh:mm:ss", sysTime);
9 mTime.setText(sysTimeStr);
10 break;
11
12 default:
13 break;
14 }
15 }
16 };
之后我们可以在Activity的onCreate方法中开启这个线程,这时我们可以看到一个数字时钟了:
1 public void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
3 setContentView(R.layout.time);
4 mTime = (TextView) findViewById(R.id.mytime);
5 new TimeThread().start();
6 }
整个Activity的代码:
1 package com.fermax.test;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.os.Handler;
6 import android.os.Message;
7 import android.text.format.DateFormat;
8 import android.widget.TextView;
9
10 public class TestActivity extends Activity {
11
12 private static final int msgKey1 = 1;
13 private TextView mTime;
14
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.time);
19 mTime = (TextView) findViewById(R.id.mytime);
20 new TimeThread().start();
21 }
22
23 public class TimeThread extends Thread {
24 @Override
25 public void run () {
26 do {
27 try {
28 Thread.sleep(1000);
29 Message msg = new Message();
30 msg.what = msgKey1;
31 mHandler.sendMessage(msg);
32 }
33 catch (InterruptedException e) {
34 e.printStackTrace();
35 }
36 } while(true);
37 }
38 }
39
40 private Handler mHandler = new Handler() {
41 @Override
42 public void handleMessage (Message msg) {
43 super.handleMessage(msg);
44 switch (msg.what) {
45 case msgKey1:
46 long sysTime = System.currentTimeMillis();
47 CharSequence sysTimeStr = DateFormat.format("hh:mm:ss", sysTime);
48 mTime.setText(sysTimeStr);
49 break;
50
51 default:
52 break;
53 }
54 }
55 };
56 }
运行结果如下图:
代码下载地址: