标签:
handler :更新UI的机制,消息的处理机制
四种用法sendMessage、sendMessageDelayed、post(runnable)、postDelayed(Runnable,long)
post用法
private android.os.Handler handler = new android.os.Handler(); new Thread() { public void run() { try { Thread.sleep(1000); handler.post(new Runnable() { @Override public void run() { text.setText("123"); } }); } catch (InterruptedException e) { e.printStackTrace(); } } }.start();
sendMessage用法
private android.os.Handler handler = new android.os.Handler() { @Override public void handleMessage(Message msg) { text.setText("" + msg.arg1); } }; new Thread() { @Override public void run() { try { Thread.sleep(1000); Message message = new Message(); message.arg1 = 100;
//handler.sendMessage(XXX);对象可以是int,也可以是obj对象
//message.obj=Person; Person是一个实体类对象 handler.sendMessage(message); } catch (InterruptedException e) { e.printStackTrace(); } } }.start();
handler.postDelayed(task,time);可以作为viewpager的图片自动切换
private android.os.Handler handler = new android.os.Handler();
private Myrunnable myrunnable = new Myrunnable(); class Myrunnable implements Runnable { @Override public void run() {
//做需要循环的操作 handler.postDelayed(myrunnable, 1000); } }
handler.postDelayed(myrunnable,1000);//调用
handler.removeCallbacks(myrunnable);//移除
标签:
原文地址:http://www.cnblogs.com/lyxin/p/5731259.html