Android中Handler的使用
2012-12-20 15:20:48 来源:WEB开发网核心提示:public class TimerActivity extends Activity { private int count = 1; private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg)
public class TimerActivity extends Activity {
private int count = 1;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
updateTitle();
break;
default:
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Message message=new Message();
message.what=1;
handler.sendMessage(message);
}
}, 1000,5000);
}
protected void updateTitle() {
setTitle("Welcome to my blog" + count++);
}
}
更多精彩
赞助商链接