package com.saul;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
/**
* 通过looper+handler方式在子线程中刷新UI
* @author Saul
*
*/
public class UpdateTitle extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ModifyTitle modifyTitle=new ModifyTitle();
modifyTitle.start();
}
class ModifyTitle extends Thread{
TitleEventHandler handler;
public void run(){
Looper mainLooper=Looper.getMainLooper();
handler=new TitleEventHandler(mainLooper);
handler.removeMessages(0);
Message msg=null;
Long currentSecond;
Long previousSecond = 0l;
int time = 5;
while (true) {
currentSecond = (System.currentTimeMillis() / 1000) % 10;
if (currentSecond != previousSecond) {
time--;
if (time < 0) {
break;
}
msg=handler.obtainMessage(1, 1, 1,"当前还剩" + (time + 1) + "秒");
handler.sendMessage(msg);
previousSecond = currentSecond;
}
}
}
}
class TitleEventHandler extends Handler{

public TitleEventHandler() {
super();
}

public TitleEventHandler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
setTitle((String)msg.obj);
}

}
}

更多相关文章

  1. Android(安卓)Service小记
  2. Android手机监控应用(一)
  3. android > WebView > 获取表单数据
  4. Android:UI更新方法一:Handler+View.invalidate+Thread+Runnable
  5. Android向桌面添加快捷方式,使其指向特定的(URL)网页
  6. Android(安卓)线程 Handler详解
  7. 在Android中使用ExecutorService、Executors、Feature
  8. android中activity如何之间互传list
  9. Android故障积累----(1/N)

随机推荐

  1. rsyslog日志服务器搭建
  2. VS Code 的 Python 扩展有大更新
  3. 【图解数据结构】 一组动画彻底理解归并
  4. 每天一算:Contains Duplicate II
  5. 每天一算:Remove Nth Node From End of Li
  6. 【图解数据结构】 一组动画彻底理解选择
  7. 每天一算:Valid Parentheses
  8. 每天一算:Reverse String
  9. 【图解数据结构】 一组动画彻底理解快速
  10. 每天一算:Evaluate Reverse Polish Notati