txt.setText(format.format(oldtime)); }
else if(msg.what==0x456) {
thread.stop(); } } };
Runnable runnable=new Runnable() { @Override public void run() { while(isstop) { try {
Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
Date newtime=new Date(oldtime.getTime()+1000); oldtime=newtime;
handler.sendEmptyMessage(0x123); }
} };
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnstart=(Button)findViewById(R.id.btnstart); btnend=(Button)findViewById(R.id.btnend); txt=(TextView)findViewById(R.id.txttime); oldtime=new Date(); oldtime.setHours(0); oldtime.setMinutes(0); oldtime.setSeconds(0);
btnstart.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) { isstop=true; if(thread==null){
thread=new Thread(runnable); thread.start(); } }
});
btnend.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) { isstop=false; }); } }
if(thread!=null){ thread.interrupt(); thread=null; } }