Ciammarica
Ciammarica

Reputation: 141

Android AlarmManager at specific date and time triggered one day before

I'm coding an alarm with AlarmManager in Android. I want to set it at a specific date and time. To check if it worked, I tried the code with today's date (Jan 9th, 2017). The problem is that the alarm isn't triggered. Instead, if I change the Calendar.DAY_OF_MONTH to 8 instead of 9, it works, as if the DAY_OF_MONTH started from 0 just as Calendar.MONTH, but I know it doesn't start from 0.

Why is this happening? Here's my code for the alarm:

private class AppointmentAlarmSetter extends AsyncTask<String, Void, Boolean>
    {
     @Override
     protected Boolean doInBackground(String... strings)
        {
         // The Alarm's Request Code
         int currentID = Constants.APPOINTMENT_ALARM_ID;

         // Start setting the alarm for current appointment
         Intent alarmIntent = new Intent(context, AlarmBroadcastReceiver.class);

         // put the RequestCode ID as intent's extra, in order to identify which alarm is triggered
         alarmIntent.putExtra("request_code", currentID);

         PendingIntent pendingIntent = PendingIntent.getBroadcast
                        (
                         context.getApplicationContext(),
                         currentID,
                         alarmIntent,
                         PendingIntent.FLAG_CANCEL_CURRENT
                        );

         Calendar calendar = Calendar.getInstance();

         calendar.setTimeInMillis(System.currentTimeMillis());

         // Specify the date/time to trigger the alarm
         calendar.set(Calendar.YEAR, 2017);
         calendar.set(Calendar.MONTH, 0);
         calendar.set(Calendar.DAY_OF_MONTH, 9);
         calendar.set(Calendar.HOUR_OF_DAY, 14);
         calendar.set(Calendar.MINUTE, 16);
         calendar.set(Calendar.SECOND, 0);

         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

         // Set the exact time to trigger the alarm
         alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

         return true;
        }
    }

Upvotes: 1

Views: 1357

Answers (2)

Ciammarica
Ciammarica

Reputation: 141

I found the mistake... In another part of my code I had these two lines:

Calendar appointmentDate = calendar;
appointmentDate.add(Calendar.DAY_OF_MONTH, 1);

By commenting them, all works correctly.

Upvotes: 1

Divyesh Patel
Divyesh Patel

Reputation: 2576

use this:

Calendar now = Calendar.getInstance();
        Calendar wakeupcall = Calendar.getInstance();
        wakeupcall.setTimeInMillis(System.currentTimeMillis());
        wakeupcall.set(Calendar.HOUR_OF_DAY, 18);
        wakeupcall.set(Calendar.MINUTE, 30);

        if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
            _alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
        else
            _alarm=wakeupcall.getTimeInMillis();


        al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        notif= new Intent(this,TestNotifyService.class);
        fintent = PendingIntent.getService(this,0,notif,0);

        if (SDK_INT < Build.VERSION_CODES.KITKAT) {
            al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
        }
        else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
            al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
        }
        else if (SDK_INT >= Build.VERSION_CODES.M) {
            al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
        }

Upvotes: 0

Related Questions