//标识位的设定
boolean flag=false;
消息的接受,UI更新或者文本更新;
Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
if(msg.what==0x01){
textView.setText((String)msg.obj);
}
}
};
//UI的初始化
flag=true;
new Thread(){
@Override
public void run() {
super.run();
while(flag){
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月 HH:mm:ss");
String timeStr =format.format(System.currentTimeMillis());
Message msg=new Message();
msg.what=0x01;
msg.obj=timeStr;
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
线程的销毁(关闭);
@Override
protected void onDestroy() {
super.onDestroy();
flag=false;
}
原文:https://www.cnblogs.com/HE-helloword/p/13519681.html