I intend to develop an application that will ask bluetooth turn on and turn off timings for days of the week from the user, and then use Alarm Service to do so. I used following grunt code for testing.
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
/*
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
*/
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 13);
cal.set(Calendar.MINUTE, 23);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
But this does not trigger my service at 1:30 PM.
The commented out code works fine. it triggers my alarms 30 mins from current time. but when i hard code the time. It doesnt work. I cant figure out the mistake in the code snippet for the Calender cal.
What am I doing wrong?
If you want a repeating alarm you should use
alarmManager.setRepeating(...)instead.alarmManager.set(...)would only trigger once.