之前有个同事,对手机进行对 手机系统的 低电量、 通知消息、充电 实现呼吸灯效果。需要用到延时处理来实现呼吸灯。所以特意的对android 实现延时总结下。言归正传,开始代码奉上。
1.使用线程的休眠实现延时操作
new Thread() {
@Override
public void run() {
super.run();
Thread.sleep(3000);//休眠3秒
/**
* 要执行的操作
*/
}
}.start();
2.使用TimerTask实现延时操作
TimerTask task = new TimerTask() {
@Override
public void run() {
/**
*要执行的操作
*/
}
};
Timer timer = new Timer();
timer.schedule(task, 3000);//3秒后执行TimeTask的run方法
3.使用Handler的postDelayed方法实现延时操作
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
/**
*要执行的操作
*/
}
}, 3000);//3秒后执行Runnable中的run方法
最后,我自己写了实例源码,把splashactivity 使用到的延时三种方法分别实现,可以根据情况来选择使用,我也在此给自己留个代码。哈哈
public class SplashActivity extends Activity implements ITaskCallBack {
private TextView tv_time;
private ImageView img_bg;
private Timer mtimer; //<span style="color: rgb(102, 102, 102); font-family: 'microsoft yahei'; line-height: 26px;">2.使用TimerTask实现延时操作
</span> private TimerTask mtimerTask;
private int TIME_COUNT = 5;
private int TIME_THREAD_COUNT = -1;
private int img_array[] = {R.drawable.splash_1,
R.drawable.splash_2,
R.drawable.splash_3,
R.drawable.splash_4};
private TimeThread mtimeThread; //<span style="color: rgb(102, 102, 102); font-family: FangSong_GB2312; line-height: 26px;">1.使用线程的休眠实现延时操作</span>
private Handler mhandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle date = msg.getData();
int d_time = date.getInt("time");
switch (msg.what){
case 0x01:
if(d_time < 4 && d_time >= 0 ){
img_bg.setBackgroundResource(img_array[d_time]);
}
tv_time.setText(d_time + "");
if(d_time == 0)
mtimer.cancel();
break;
case 0x02:
if(d_time < 4 && d_time >= 0 ){
img_bg.setBackgroundResource(img_array[d_time]);
}
tv_time.setText(d_time + "");
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay_splash);
tv_time = (TextView) findViewById(R.id.splash_time);
tv_time.setText(TIME_COUNT + "");
img_bg = (ImageView) findViewById(R.id.splash_bg);
mtimer = new Timer();
/* mtimerTask = new TimerTask() {
@Override
public void run() {
TIME_COUNT --;
Message msg = new Message();
Bundle bud = new Bundle();
bud.putInt("time", TIME_COUNT);
msg.setData(bud);
msg.what = 0x01;
mhandler.sendMessage(msg);
}
};
mtimer.schedule(mtimerTask, 1000, 2000);
*/
/*Task task = new Task(this);
mtimer.schedule(task, 1000, 2000);*/
mtimeThread = new TimeThread(mhandler, TIME_THREAD_COUNT);
mtimeThread.start();
}
@Override
public void taskRun() {
TIME_COUNT --;
Message msg = new Message();
Bundle bud = new Bundle();
bud.putInt("time", TIME_COUNT);
msg.setData(bud);
msg.what = 0x01;
mhandler.sendMessage(msg);
}
上面的代码用使用回调接口, 以及线程
public interface ITaskCallBack {
void taskRun();
}
public class Task extends TimerTask {
ITaskCallBack callback;
public void setCallBack(ITaskCallBack callback){
this.callback = callback;
}
public Task(ITaskCallBack iTaskcallback) {
this.callback = iTaskcallback;
}
@Override
public void run() {
callback.taskRun();
}
}
mtimeThread = new TimeThread(mhandler, TIME_THREAD_COUNT);
mtimeThread.start();
线程来实现延时。
public class TimeThread extends Thread {
private Handler mhandler;
private int mCount = -1;
public TimeThread(Handler handler, int count) {
this.mhandler = handler;
this.mCount = count;
}
@Override
public void run() {
super.run();
while(true){
Message message = new Message(); //特别要注意, message 一定要放到 while 循环里面,否则会报错,messagequeue 一直在用。
Bundle bund = new Bundle();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCount ++ ;
bund.putInt("time", mCount);
message.setData(bund);
message.what = 0x02;
mhandler.obtainMessage();
mhandler.sendMessage(message);
if(mCount == 5)
break;
}
}
}
最后奉上 通过延时实现应用引导页的效果 源码