手机端更新系统时间显示在客户端

定义一个Activity来实现更新系统时间显示在手机端 

RegistrationActivity

package cn.hwttnet.com.ui;

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

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

public class RegistrationActivity extends Activity {
	private static SimpleDateFormat currentTime;
	private static TextView systemTime;
	private LooperThread mClockThread;
	private static String date;
	public static Handler m_Handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				systemTime.setText(date);
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registration);
		systemTime = (TextView) findViewById(R.id.registration_time);

		mClockThread = new LooperThread();
		mClockThread.start();

	}

	class LooperThread extends Thread {
		@Override
		public void run() {
			super.run();
			try {
				do {
					currentTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");
					date = currentTime.format(new java.util.Date());
					Thread.sleep(1000);
					Message m = new Message();
					RegistrationActivity.m_Handler.sendMessage(m);
				} while (!LooperThread.interrupted());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

相关推荐