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; } } }; }
附图:
时间: 2024-10-13 15:00:15