I'm making an alarm application. Alarm is running ok. For example If I set the alarm at 06:00 PM it executes at 06:00 PM. The problem is that alarm is not executing when seconds are exactly zero. Sometimes it's running at 32 seconds (06:00:32 PM) sometimes at other seconds etc.
In my main activity, I have set alarm save button, when I click on that the following code executes:
buttonSetAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(cal);
}
}});
SetAlarm Method inside main activity is:
private void setAlarm(Calendar targetCal){
info.setText("\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 1000*60, pendingIntent);
}
In my AlarmReceiver BroadCastReceiver I have following onReceive Method:
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
}
Ideas on where my problem should be?
Starting with API 19 (KitKat), alarms are inexact by default. If your targetSdkVersion
is set to 19 or higher, this behavior will be used. See the note in the docs for the AlarmManager.setRepeating()
method:
Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
So your best bet is to use exact one-shot alarms and each time it expires set your alarm again accordingly.