Android 动态显示时间

package code.example.newcodeview;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

	private static final int msgKey = 1;
	private TextView txtTime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		txtTime = (TextView) findViewById(R.id.txtSystemtime);
		new TimeThread().start();
	}

	public class TimeThread extends Thread {
		@Override
		public void run() {
			do {
				try {
					Thread.sleep(1000);
					Message msg = new Message();
					msg.what = msgKey;
					mHandler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} while (true);
		}
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case msgKey:
				long sysTime = System.currentTimeMillis();
				CharSequence sysTimeStr = DateFormat
						.format("hh:mm:ss", sysTime);
				txtTime.setText("北京时间:" + sysTimeStr);
				break;
			default:
				break;
			}
		}
	};
}

  附图:
技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。