Here is the code that I used to set an alarm for my widget:
private static void setAlarm(Context context) {
Intent myIntent = new Intent(context, Widget.class);
myIntent.setAction(AUTO_UPDATE);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 8);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
pendingIntent);
}
but the problem is that even in the sleep mode, onReceive()
is still triggered by the intent.
Although after using setInexactRepeating
instead of setRepeating
, the delays between calls get increased up to 1 minute in sleep mode, but that's still battery consuming.