Android AlarmManager 设定过去的时间会触发事件

Android AlarmManager 设定过去的时间会触发事件

在使用 AlarmManager 做每日定时任务时,发现如果设定的时间小于当前的系统时间,那么设定后会立刻收到一次定时任务回调。

我们设想的是设定的时间应该是明日的这个时间,但是如果打印出设定的时间会发现,设定的时间是今天的。

java 复制代码
public void addTask() {
        Calendar calendar = Calendar.getInstance();
        // 设置时区,否则会有 8 个小时的时间差
        calendar.setTimeZone(TimeZone.getDefault());

        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 0);

        long setTime = calendar.getTimeInMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Log.e("WillWolf", "setTime-->" + sdf.format(new Date(setTime)));
        long currentTime = System.currentTimeMillis();
        Log.e("WillWolf", "currentTime-->" + sdf.format(new Date()));

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTime().getTime(), "TAG", listener, null);
    }

日志输出为:

java 复制代码
setTime-->2024-06-21 15:30:00
currentTime-->2024-06-21 17:31:02

所以,为了避免时间已经过去还引发回调,可以添加一个时间判断。

java 复制代码
public void addTask() {
        Calendar calendar = Calendar.getInstance();
        // 设置时区,否则会有 8 个小时的时间差
        calendar.setTimeZone(TimeZone.getDefault());

        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 0);

        long setTime = calendar.getTimeInMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Log.e("WillWolf", "setTime-->" + sdf.format(new Date(setTime)));
        long currentTime = System.currentTimeMillis();
        Log.e("WillWolf", "currentTime-->" + sdf.format(new Date()));
        // 如果当前的时间大于设定的时间,表明设定的时间是未来一天的,做一个加一天的操作
        if (currentTime >= setTime) {
            calendar.add(Calendar.HOUR, 24);
            setTime = calendar.getTimeInMillis();
            Log.e("WillWolf", "setTime tomorrow-->" + sdf.format(new Date(setTime)));
        }

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTime().getTime(), "TAG", listener, null);
    }

日志输出为:

java 复制代码
setTime-->2024-06-21 15:30:00
currentTime-->2024-06-21 17:31:02
setTime tomorrow-->2024-06-22 15:30:00

这样设定的触发时间就是明天的了。

相关推荐
_一条咸鱼_42 分钟前
深度揭秘!Android HorizontalScrollView 使用原理全解析
android·面试·android jetpack
_一条咸鱼_43 分钟前
揭秘 Android RippleDrawable:深入解析使用原理
android·面试·android jetpack
_一条咸鱼_43 分钟前
深入剖析:Android Snackbar 使用原理的源码级探秘
android·面试·android jetpack
_一条咸鱼_43 分钟前
揭秘 Android FloatingActionButton:从入门到源码深度剖析
android·面试·android jetpack
_一条咸鱼_44 分钟前
深度剖析 Android SmartRefreshLayout:原理、源码与实战
android·面试·android jetpack
_一条咸鱼_1 小时前
揭秘 Android GestureDetector:深入剖析使用原理
android·面试·android jetpack
_一条咸鱼_1 小时前
深入探秘 Android DrawerLayout:源码级使用原理剖析
android·面试·android jetpack
_一条咸鱼_1 小时前
深度揭秘:Android CardView 使用原理的源码级剖析
android·面试·android jetpack
_一条咸鱼_1 小时前
惊爆!Android RecyclerView 性能优化全解析
android·面试·android jetpack
_一条咸鱼_1 小时前
探秘 Android RecyclerView 惯性滑动:从源码剖析到实践原理
android·面试·android jetpack