Android线程显示数字时钟

package com.example.datetest;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Thread clockThread;
	private boolean isRunning = true;
	private Handler handler;

	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;

	TextView text1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		text1 = (TextView) findViewById(R.id.textView1);

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);

		String YY = String.valueOf(mYear);
		String MM = String.valueOf(mMonth);
		String DD = String.valueOf(mDay);
		String hh = String.valueOf(mHour);
		String mm = String.valueOf(mMinute);

		SimpleDateFormat dateformat1 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss a E");

		Date d = new Date();

		ContentResolver cv = this.getContentResolver();
		final String strTimeFormat = android.provider.Settings.System
				.getString(cv, android.provider.Settings.System.TIME_12_24);

		// if (strTimeFormat.equals("12")) {
		//
		// SimpleDateFormat dateformat2 = new SimpleDateFormat(
		// "yyyy-MM-dd a hh:mm:ss  E");
		// String a1 = dateformat2.format(d);
		// text1.setText(a1);
		// }
		//
		// else if (strTimeFormat.equals("24")) {
		//
		// SimpleDateFormat dateformat3 = new SimpleDateFormat(
		// "yyyy-MM-dd  HH:mm:ss  E");
		// String a1 = dateformat3.format(d);
		// text1.setText(a1);
		// }
		//

		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					SimpleDateFormat dateformat1 = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss a E");
					String a1 = dateformat1.format(msg.obj);
					text1.setText(a1);
				}
			}
		};

		clockThread = new Thread(new Runnable() {
			@Override
			public void run() {

				while (isRunning) {
					try {
						Thread.currentThread().sleep(1000);

						Message msg = new Message();
						msg.obj = new Date();
						msg.what = 0;
						handler.sendMessage(msg);

					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});

		clockThread.start();

		// G 年代标志符
		// y 年
		// M 月
		// d 日
		// h 时 在上午或下午 (1~12)
		// H 时 在一天中 (0~23)
		// m 分
		// s 秒
		// S 毫秒
		// E 星期
		// D 一年中的第几天
		// F 一月中第几个星期几
		// w 一年中第几个星期
		// W 一月中第几个星期
		// a 上午 / 下午 标记符
		// k 时 在一天中 (1~24)
		// K 时 在上午或下午 (0~11)
		// z 时区

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 参考资料:http://tech.it168.com/a2011/0922/1250/000001250289_all.shtml

你可能感兴趣的:(android)