I have this problem with inconsistency of launching alarms while device is in sleep mode (well documented problem), that is driving me crazy.
In my app for frequent reminders i use alarmmanager to set time for next reminder, when the previous one occurs. In most cases app runs fine, but in sleep mode on my ASUS TF300t (API 17), the alarms goes bonkers.
Code for registering alarm:
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RReminder.PERIOD_TYPE, type);
i.putExtra(RReminder.EXTEND_COUNT, extendCount);
i.setAction(RReminder.CUSTOM_INTENT_ALARM_PERIOD_END);
pi = PendingIntent.getBroadcast(mContext, (int)when, i, PendingIntent.FLAG_ONE_SHOT);
if(buildNumber >= Build.VERSION_CODES.LOLLIPOP){
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(when, pi);
mAlarmManager.setAlarmClock(alarmClockInfo,pi);
} else if(buildNumber >= Build.VERSION_CODES.KITKAT){
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, when, pi);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when, pi);
}
And my WakefulBroadcastReceiver class:
public class OnAlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
int type = intent.getExtras().getInt(RReminder.PERIOD_TYPE);
int extendCount = intent.getExtras().getInt(RReminder.EXTEND_COUNT);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Log.d("period type",""+type);
Log.d("time",sdf.format(Calendar.getInstance().getTime()));
Intent i = new Intent(context, PeriodService.class);
i.putExtra(RReminder.PERIOD_TYPE, type);
i.putExtra(RReminder.EXTEND_COUNT, extendCount);
startWakefulService(context,i);
}
}
First thing i am observing is the alarm-batching, when an alarm isn't launched when it was supposed, but instead it launches when the next alarm is due together with the second one.
Other thing is, when an alarm does not launch on time in sleep mode, it goes off right after i wake up the device with power button.
I am aware of the changes of alarm behaviour in API 19 and 23 (doze mode). I solved the inexact issues with newer API's observed in emulated virtual devices with setExact().
I have two physical android devices, this TF300t tablet and Galaxy S3 phone. And while with S3 this problem is occurs very rarely, the consistency of problems on TF300t basically makes my app useless.
So my question would be, are my described problems present on many devices across all API's (which would require me to look for entireally different solution), or are they device or API specific and can be dealt as exception?