//主要使用子线程发送消息 使用Handler更新显示 import java.text.SimpleDateFormat; import java.util.Date; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.widget.TextView; public class MainActivity extends Activity implements Runnable{ private Handler handler; private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView=(TextView) findViewById(R.id.time); handler = new Handler() { public void handleMessage(Message msg) { textView.setText((String)msg.obj); } }; new Thread(this).start(); } @Override public void run() { // TODO Auto-generated method stub try { while(true){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String str=sdf.format(new Date()); handler.sendMessage(handler.obtainMessage(100,str)); Thread.sleep(1000); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
android开发中动态获取系统时间
最新推荐文章于 2024-02-05 17:50:16 发布