Android Alarm Manager不适用于特定日期和时间 [英] Android Alarm Manager not working with specific date and time

查看:96
本文介绍了Android Alarm Manager不适用于特定日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在研究药物摄入应用程序,我需要在本地提醒用户(无需互联网/推送通知)有关服用药物的信息。
我为此使用了Android Alarm Manager。下面是代码
注意:我正在尝试为特定日期安排警报: 2018年7月13日下午3点30分 。我安排并等待,但提醒未触发(因此未广播),但是如果我使用 AlarmManager.ELAPSED_REALTIME_WAKEUP 且已定义的毫秒数,则会触发(但AlarmManager.RTC_WAKEUP >只是不起作用)
`

So I have been working on medication intake app, where I need to remind the user locally ( no internet/push notification needed) about taking their medication. I am using Android Alarm manager for this. Below is the code Note I am trying to schedule the alarm for a specific date: "13 July 2018 at 3h30 PM". I schedule and wait but the reminder didn't fire (so not broadcast) however if I used AlarmManager.ELAPSED_REALTIME_WAKEUP with a defined amount of millisecond it does fire ( but AlarmManager.RTC_WAKEUP just does not work) `

    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent myIntent;
    PendingIntent pendingIntent;
    long reminderDateTimeInMilliseconds = 000;

    myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);

    pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    //TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
    Calendar calendarToSchedule = Calendar.getInstance();

    calendarToSchedule.set(Calendar.YEAR, 2018);
    calendarToSchedule.set(Calendar.MONTH, 07);
    calendarToSchedule.set(Calendar.DAY_OF_MONTH, 13);
    calendarToSchedule.set(Calendar.HOUR_OF_DAY, 15);
    calendarToSchedule.set(Calendar.MINUTE, 30);

    reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){

        manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
    }
    else{

        manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
    }

`


推荐答案

所以我弄清楚了这个问题,主要的问题是Java中的日历月实际上是基于0索引的,所以(Jan:0-Dec:11)所以下面是更新的代码。

So I figure out the issue, the main issue was the calendar month in Java is actually 0 index based so ( Jan:0 - Dec:11) so below is the updated code.

AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
long reminderDateTimeInMilliseconds = 000;

myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);

pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

//TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
// Note: For the month of July the int value will actuall be 6 instead of 7
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
calendarToSchedule.clear();

//.Set(Year, Month, Day, Hour, Minutes, Seconds);
calendarToSchedule.set(2018, 06, 13, 15, 30, 0);


reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){

    manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
else{

    manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}

这篇关于Android Alarm Manager不适用于特定日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆