首页 > 解决方案 > Alarmmanager setExact 方法无法正常工作

问题描述

Hı 我正在使用 Alarmmanager 来运行我的广播接收器代码,但是当添加过去时间的日历时(例如,如果当前时间是 15:32并且我

calendar.set(Calendar.HOUR_OF_DAY, 13);
            calendar.set(Calendar.MINUTE, 7);
            calendar.set(Calendar.SECOND,23);

使用上面的代码,我的广播接收器调用每个 setupAlarm 方法,如果我

calendar.set(Calendar.HOUR_OF_DAY, 16);
                calendar.set(Calendar.MINUTE, 7);
                calendar.set(Calendar.SECOND,23);

使用这一切都很好,有人知道原因吗?

` private void setupAlarm(Context context) {
        Intent myIntent = new Intent(context, MyBroadcastReceiver.class);
        PendingIntent myPendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
        // Set the alarm to start at 8:30 a.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 7);
        calendar.set(Calendar.SECOND,23);
        AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmMgr != null) {
            alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), myPendingIntent);
        }
    }`

标签: androidalarmmanager

解决方案


推荐阅读