问题描述
- android中的延迟动作
-
代码实现预期功能是:在应用开始五秒钟后,变化imageView的图片。public class MainActivity extends Activity { ImageView screen; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); screen=(ImageView)findViewById(R.id.imageView1); screen.setImageResource(R.drawable.ic_launcher); } }
应该怎么成功延迟?谢谢帮忙
解决方案
可以通过Handler来实现
Handler定义:
class ReadDataHandler extends Handler{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
updateBlackBackground();
updateWhiteBackground();
super.handleMessage(msg);
}
}
Handler使用:
private ReadDataHandler mReadDataHandler;
mReadDataHandler = new ReadDataHandler();
需要处理时发送消息:
Message lMessage = new Message();
mReadDataHandler.sendMessageDelayed(lMessage, 5000);
解决方案二:
不知多线程能不能满足你呢,这些延迟刷新的要放到新的线程里才不对主进程造成阻塞
new Thread(){
public void run(){
//sleep(5000);
//refreshSthHere();
}
}.start();
解决方案三:
几种方式,最常用的是:
1. handler.sendMessageDelayed or handler.postRunnableDelayed
2. alarmmanager.set
时间: 2024-11-10 00:58:41