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;

}

}

};

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐