Android延时的三种方式

xiaoxiao2021-02-28  80

App开发在某些情况下需要有延时功能,比如说App首页显示定格3秒,然后自动跳到登录页的情况,这就好比是一个预加载,但是这个预加载可能瞬间就完成了,撑不到3秒钟,这是就要求你做延时处理。

下面是三种方法:

 

一、线程

 

[html] view plain copy 1. new Thread(new Runnable(){    2.     public void run(){    3.         Thread.sleep(XXXX);    4.         handler.sendMessage();----告诉主线程执行任务    5.     }    6. }).start    

 

二、延时器

 

[html] view plain copy 1. TimerTask task = new TimerTask(){    2.     public void run(){    3.     //execute the task     4.     }    5. };    6. Timer timer = new Timer();     timer.schedule(task, delay);  

 

三、android消息处理

 

[html] view plain copy new Handler().postDelayed(new Runnable(){        public void run() {        //execute the task        }     }, delay);   

推荐使用第三种

转载请注明原文地址: https://www.6miu.com/read-52785.html

最新回复(0)