获取Calendar的时钟信息,显示到自定义的Text View,中并每隔一分中刷新一下。
public class MainActivity extends Activity {
Calendar clock = null;private TextView clock_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clock_info = (TextView)findViewById(R.id.clock);
clock_info.setTextColor(Color.BLACK);
clock_info.setTextSize(20);
clock = Calendar.getInstance();
clock_info.setText(""+clock.get(Calendar.HOUR_OF_DAY)+clock.get(Calendar.MINUTE));
startSendMessage();
}
private Handler m_Handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
startSendMessage();
}
};
private void startSendMessage() {
Message msg = m_Handler.obtainMessage();
//msg.arg1 = 123;//传递整型数据
//msg.obj = "asd";传递object类型
//利用bundle对象来传值
Bundle b = new Bundle();
Calendar clock = Calendar.getInstance();
b.putInt("hour", clock.get(Calendar.HOUR_OF_DAY));
b.putInt("minute", clock.get(Calendar.MINUTE));
b.putInt("second", clock.get(Calendar.SECOND));
clock_info.setText(""+b.getInt("hour")+": "+b.getInt("minute"));
msg.setData(b);
m_Handler.sendMessageDelayed(msg, 60000);
}