private TextView textView;
static boolean showtime=true;
private android.os.Handler handler=new android.os.Handler(){
public void handleMessage(Message msg){
switch (msg.what){
case 0x123:
textView.setText(msg.obj.toString());break;
case 0x1234:
textView.setText("OK?");break;
default:break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Button button=(Button)findViewById(R.id.button_top);
textView=(TextView)findViewById(R.id.text_top);
final Thread threadShowTime=new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = 0x123;
msg.obj = new Date().toString();
Message msg1=new Message();
msg1.what=0x1234;
if(showtime)
handler.sendMessage(msg);
else
handler.sendMessage(msg1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
threadShowTime.start();
if (showtime) {
showtime = false;
String s=String.valueOf(showtime);
Toast.makeText(FirstActivity.this,s,Toast.LENGTH_SHORT).show();
} else {
threadShowTime.interrupt();
showtime = true;
String s=String.valueOf(showtime);
Toast.makeText(FirstActivity.this,s,Toast.LENGTH_SHORT).show();
}
}
});