Android: timed tasks

article reference

When you need to design scrolling notes or pictures, you need to design a timer to make it switch regularly to form scrolling playback. Therefore, a timer mechanism is needed. The method is as follows:

  • Handler+sleep
  • Handler.postDelayed(runable,time)
  • Handler+timer/timerTask
  • CountDownTimeActivity

Hours/hoursTask

Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
    
    
    @Override
    public void run() {
    
    
        mTvShow.setText(String.valueOf(counts++));
    }
};
//timerTask:周期执行代码
//0:指定延时多少毫秒开始执行,延迟时间
//1000:指定每隔多少毫秒执行一次,周期时间
timer.schedule(timerTask,0,1000);//schedule有好几个重载
timer.scheduleAtFixedRate()//跟上面相仿,但固定有三个参数
timer.cancel();//将timerTask变为取消态,timerTask执行完当次后结束线程
timer.purge();//回收已经处于取消态的timerTask

CountDownTimer

//10000:倒计时总时间
//1000:倒计时间隔
CountDownTimer countDownTimer = new CountDownTimer(10000,1000) {
    
    
    @Override
    public void onTick(long l) {
    
    
        //此处为任务每完成一次倒计时间隔时回调
        //l参数是当前计时任务的进度:
        //在这个例子中的值是:9000 8000 7000 6000等
        //这里面是可以直接更新UI的
        mTvShow.setText(String.valueOf(counts++));
    }

    //时间段内最后一次定时任务完成时回调
    @Override
    public void onFinish() {
    
    
        mTvShow.setText(String.valueOf(counts++) + " finished!");
    }
};
countDownTimer.start();//开启任务

thread sleep

new Thread(new Runnable() {
    
    
    @Override
    public void run() {
    
    
        try {
    
    
            Thread.sleep(1000);//延时1s
            //do something
            //不能在此更新UI,需要在主线程中更新UI
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
}).start();

Hanlder的postDelayed

//只启动一次
new Handler().postDelayed(new Runnable() {
    
    
    @Override
    public void run() {
    
    
        //do something
    }
}, 1000);    //延时1s执行

//一直重复
Handler handler = new Handler();
Runnable runnable = new Runnable() {
    
    
    @Override
    public void run() {
    
    
        Log.d(TAG, "run: " + String.valueOf(counts++));
        handler.postDelayed(this,1000);
    }
};
handler.postDelayed(runnable,50);//启动定时任务
//使用下面方式停止定时任务
handler.removeCallbacks(runnable);

AlarmManager

//编写广播接收
class MyTimerReceiver extends BroadcastReceiver{
    
    
   @Override
    public void onReceive(Context context, Intent intent) {
    
    
        Log.d(TAG, "onReceive: " + String.valueOf(counts++));
    }
}
//设置任务并且注册广播
Intent intent = new Intent();
intent.setAction("com.mt.timer.test");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//从API 19开始,所有重复警报都是不准确的。
//如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,如上所述每次重新安排时间。
//在设置间隔时间时,系统默认为至少60秒,设置少于60秒时,按照60秒为间隔时间,当大于60秒时,按照设置的时间为间隔时间。
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//setRepeating参数有四个:
//ELAPSED_REALTIME、ELAPSED_REALTIME_WAKEUP、RTC、RTC_WAKEUP
//分别为绝对时间是否唤醒系统和相对时间是否唤醒系统
//延迟时间
//执行间隔时间
//pendingtintent对象
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,0,10,pendingIntent);
myTimerReceiver = new MyTimerReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.mt.timer.test");
registerReceiver(myTimerReceiver,intentFilter);
//退出任务:
//通过该方式实现的定时器任务,当应用退出后,该定时器任务也不会结束,唯一的结束方法就是通过代码去结束。
alarmManager.cancel(pendingIntent);

Guess you like

Origin blog.csdn.net/weixin_51109304/article/details/131133336