0


Android——常用定时器

文章目录


Timer和TimerTask

这是常规的实现方式,对于大多数人的选择都会采用这种方式实现定时任务。这种实现方式的生命周期和Acticity的生命周期一样,当Activity销毁后,该定时任务也会结束。即退出该应用时,定时任务结束。

Timer timer =newTimer();TimerTask timerTask =newTimerTask(){@Overridepublicvoidrun(){
        mTvShow.setText(String.valueOf(counts++));}};//参数说明://第二个:指定延时多少毫秒开始执行//第三个:指定每隔多少毫秒执行一次
timer.schedule(timerTask,0,1000);

自带定时器 CountDownTimer

常用方法:
方法1:cancel(): 取消当前的任务
方法2:onFinish(): 当前任务完成的时候回调
方法3:onTick(long millisUntilFinished): 当前任务每完成一次倒计时间隔时间时回调
方法4:start(): 开始当前的任务

CountDownTimer countDownTimer =newCountDownTimer(10000,1000){@OverridepublicvoidonTick(long l){//l参数是当前计时任务的进度://在这个例子中的值是:9000 8000 7000 6000等//这里面是可以直接更新UI的
        mTvShow.setText(String.valueOf(counts++));}//时间段内最后一次定时任务@OverridepublicvoidonFinish(){
        mTvShow.setText(String.valueOf(counts++)+" finished!");}};
countDownTimer.start();

使用普通子线程延时

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

使用Hanlder的postDelayed方法

//只启动一次newHandler().postDelayed(newRunnable(){@Overridepublicvoidrun(){//do something}},1000);//延时1s执行//一直重复Handler handler =newHandler();Runnable runnable =newRunnable(){@Overridepublicvoidrun(){Log.d(TAG,"run: "+String.valueOf(counts++));
        handler.postDelayed(this,1000);}};
handler.postDelayed(runnable,50);//启动定时任务//使用下面方式停止定时任务
handler.removeCallbacks(runnable);

使用系统的AlarmManager来实现定时任务

在使用AlarmManager来设置定时器时,在调用方法setRepeating()方法时会传入四个参数,这些参数的含义如下:
type:这个参数可以设置4种类型
ELAPSED_REALTIME:当系统进入休眠状态时,这种类型的时钟不会唤醒系统。直到系统下次被唤醒才会传递它,该闹钟所使用的是绝对时间,从系统启动开始计时的,可以通过调用SystemClock.elapsedRealtime()获得。系统值是3。
ELAPSED_REALTIME_WAKEUP:当系统进入休眠状态,这种类型的闹钟可以唤醒。系统值是2。
RTC:当系统进入休眠状态时,这种类型的时钟不会唤醒系统。直到系统下次被唤醒才会传递它。该闹钟所使用的是相对时间。可以调用System.currentTimeMillis()获得。系统值是 1。
RTC_WAKEUP:当系统进入休眠状态时,这种类型的时钟会唤醒系统。系统值是0。设为该模式下除了基本的定时器功能外,还会发出警报声,例如响铃、震动。
trrggerAtTime 第一次运行等待的时间,也就是执行延迟时间,单位是毫秒。
interval 表示执行的间隔时间,单位是毫秒。在设置间隔时间时,系统默认为至少60秒,设置少于60秒时,按照60秒为间隔时间,当大于60秒时,按照设置的时间为间隔时间。
operation一个PendingIntent对象,表示到时间后要执行的操作。PendingIntent与Intent类似,可以封装Activity、BroadcastReceiver和Service。但与Intent不同的是,PendingIntent可以脱离应用程序而存在。

//编写广播接收classMyTimerReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Context context,Intent intent){Log.d(TAG,"onReceive: "+String.valueOf(counts++));}}//设置任务并且注册广播Intent intent =newIntent();
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);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,0,10,pendingIntent);
myTimerReceiver =newMyTimerReceiver();IntentFilter intentFilter =newIntentFilter();
intentFilter.addAction("com.mt.timer.test");registerReceiver(myTimerReceiver,intentFilter);//退出任务://通过该方式实现的定时器任务,当应用退出后,该定时器任务也不会结束,唯一的结束方法就是通过代码去结束。
alarmManager.cancel(pendingIntent);

标签: android ui java

本文转载自: https://blog.csdn.net/qq_43745685/article/details/126617335
版权归原作者 mt_procode 所有, 如有侵权,请联系我们删除。

“Android——常用定时器”的评论:

还没有评论