简体   繁体   中英

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. 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. It's a holdover from Java date/calendar.

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 = 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.

When you set the values for Calendar it is expecting a 0-indexed month. You're giving it a 1-indexed month. So you'll need to subtract 1 from the month to get the right value. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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