简体   繁体   English

根据数据库中的日期设置警报

[英]Setting an alarm based on date in database

I found a tutorial that helped me get a working alarm setup but in the tutorial we used a date picker to set the time of the alarm. 我找到了可以帮助我设置正常警报的教程,但是在该教程中,我们使用了日期选择器来设置警报时间。 All was going well, so I attempted to replace the date picker with a method of my own and set the alarm that way. 一切进展顺利,因此我尝试用自己的方法替换日期选择器,并以此方式设置警报。 Problem is it doesn't seem to be working, no alarm pops up when I set that date to today but works fine when I use the date picker. 问题是它似乎不起作用,当我将该日期设置为今天时没有警报弹出,但是当我使用日期选择器时可以正常工作。 Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错? I ran this through debug and my query is returning the proper date, I'm getting the right numbers and all but it doesn't seem to work. 我通过调试运行了这个查询,查询返回的是正确的日期,我得到了正确的数字,但所有这些似乎都不起作用。

I'm hoping someone can point out something I've missed? 我希望有人能指出我错过的事情吗?

EDIT 编辑

I call setNotif() through a button in the ui. 我通过ui中的按钮调用setNotif() I basically remodelled it to just have a button for testing. 我基本上将其重塑为仅具有一个测试按钮。 I get the toast but no alarm goes off no matter what I try. 我干杯,但无论尝试如何,都不会响起警报。

Here's the original date picker code 这是原始的日期选择器代码

public void onDateSelectedButtonClick(View v){

    int day = picker.getDayOfMonth();
    int month = picker.getMonth();
    int year = picker.getYear();

    Calendar c = Calendar.getInstance();
    c.set(year, month, day);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);

    scheduleClient.setAlarmForNotification(c);


    Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}

Here's the method I tried to replace it with 这是我尝试替换为的方法

It calls a query to get a specific date from my database and supposedly sets an alarm 3 days prior. 它调用查询以从我的数据库中获取特定日期,并且应该在三天前设置警报。 I tested it by changing the date of my phone and by making it set the alarm to today as well but neither have worked. 我通过更改手机日期并将其设置为今天的闹钟来对其进行测试,但均未奏效。

public void setNotif() {

    // Getting the next date of payments which the alarm will be based on
    DatabaseHelper db = new DatabaseHelper(this);

    String thisdate = getCurDate();
    Cursor cur = db.nextdate(thisdate);
    String date= "";
    if (cur!= null) {
        if (cur.moveToFirst()) {
            date = cur.getString(0);
        }
    }
    try {
        DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date input = inputFormat.parse(date);
        String finaldate = inputFormat.format(input);
        String d = finaldate.substring(8,10);
        String m = finaldate.substring(5,7);
        String y = finaldate.substring(0,4);
        int da = Integer.parseInt(d);
        int month = Integer.parseInt(m);
        int year = Integer.parseInt(y);
        int day = da - 3;

        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        scheduleClient.setAlarmForNotification(c);

        Toast.makeText(this, "Notification set for: "+ day +"/"+ month +"/"+ year, Toast.LENGTH_SHORT).show();
    }catch (Exception ex) {
        Alerts.CatchError(Kik.this, ex.toString());
    }

}

This is an easy road block to run into, unfortunately and it doesn't really have anything to do with Android. 这是一个简单的路障碰上了,不幸的是,它并没有真正有什么做的Android。 It's a holdover from Java date/calendar. 这是Java日期/日历的保留。

When you pull the date out of the int month out of the date picker in the example code the value you get out is 0 based: 当您从示例代码中的日期选择器中将日期从int month拉出时,得出的值基于0:

int month = picker.getMonth();
// January -> 0
// February -> 1
// ...
// December -> 11

The dates in your database are dates we're used to looking at: 2016-12-25 is December 25th, 2016. 您数据库中的日期是我们习惯查看的日期: 2016-12-25是2016年12月25日。

When you set the values for Calendar it is expecting a 0-indexed month. 当您设置Calendar的值时,它期望索引为0的月份。 You're giving it a 1-indexed month. 您给它一个索引为1的月份。 So you'll need to subtract 1 from the month to get the right value. 因此,您需要从月份中减去1以获得正确的值。 I'll make a table so it's clear what's going on: 我将制作一张桌子,这样很清楚发生了什么事:

Month String  |  Database Value  |  Calendar Value 
January       |  1               |  0
February      |  2               |  1
March         |  3               |  2
April         |  4               |  3
May           |  5               |  4
June          |  6               |  5

Hopefully that makes it obvious. 希望这很明显。

So in your case if you change the line: 因此,在您的情况下,如果您更改行:

c.set(year, month, day);

To be... 成为...

c.set(year, month - 1, day);

You'll win. 你会赢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM