有两种方式:
第一种:post给Handler
// 开启线程刷新 handler = new Handler(); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { mTextLat.setText(AppContext.getLatitude()+""); mTextLng.setText(AppContext.getLongitude()+""); } }); } },0,1*1000);
第二种:给Handler发信息
// 开启线程刷新 handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: mTextLat.setText(AppContext.getLatitude()+""); mTextLng.setText(AppContext.getLongitude()+""); } super.handleMessage(msg); } }; timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Message msg = new Message(); msg.what = 1; handler.sendMessage(msg); } },0,1*1000);
TimerTask中如何通过Handler交由UIThread更新UI
原文地址:http://blog.csdn.net/zsg2063/article/details/40661397