A
A
Anton Akimov2017-04-24 17:54:55
Android
Anton Akimov, 2017-04-24 17:54:55

How to make a sleeping Android device periodically wake up and do useful work?

The idea is that the application should periodically (every 2-10 minutes) wake up the sleeping device, do some work, and then let the device sleep further.
According to the official documentation, the AlarmManager and its setExactAndAllowWhileIdle(…) method are best suited for this purpose. With a normal battery charge, it works with an accuracy of about 1 minute. In Doze mode, these alarms cannot go off more than once every 9 minutes (other sources say 15 minutes). However, an application can be added to a "Whitelist" that will allow it to ignore Doze mode.
The point is, it's all theory. In practice, it turns out that Android, in any case, acts at its own discretion, regardless of the "Accurate alarm clock", or the "White List", or the frequency of alarm calls. With one launch of the application, it executes all alarms for an hour and a half in a row; on the other hand, it performs only one or two of them, and the next one postpones until the device is manually unlocked, even if it happens only an hour later.
This is how my alarm clock looks like:

AlarmManager alarmManager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(appContext, WakeUpReceiver.class);
PendingIntent alarmPIntent = PendingIntent.getBroadcast(appContext, 0, alarmIntent, 0);
long trigger = System.currentTimeMillis() + 120_000;
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, trigger, alarmPIntent);

When the alarm goes off, I do my job and reset the alarm.
If someone had to work with alarm clocks, share your experience, if you had similar problems and how you solved them. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-04-24
@TrueBers

Nothing will work exactly after the 7th version of the android. This is done on purpose so that especially crooked developers do not gobble up the battery for each sneeze. It is currently recommended to use JobScheduler .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question