scheduling alarm for every second in android 5.1
This is normal behavior in Android Lollipop.
Suspiciously short interval 1000 millis; expanding to 60 seconds
Tells you that the system does not like those short time intervals anymore.
Issue #161244 documented that:
This is working as intended, though is at present inadequately documented (and we're aware of that side of the problem).
Speaking very generally: short-period and near-future alarms are startlingly costly in battery; apps that require short-period or near-future work should use other mechanisms to schedule their activity.
So don't use an AlarmService
for this. Prefer a thread or Executors
or TimerTask
or something else:
// Using Handlernew Handler().postDelayed(runnable, TimeUnit.SECONDS.toMillis(1));// Using ExecutorsExecutors.newSingleThreadScheduledExecutor().schedule(runnable, 1, TimeUnit.SECONDS);
Why would you do that?
Use an handler instead:
Runnable runnable = new Runnable() { @Override public void run() { // do your stuff here, called every second mHandler.postDelayed(this, 1000); }};// start it with:mHandler.post(runnable);
And use the following to stop your 1 sec timer:
mHandler.removeCallbacks(runnable);
Use both 1 and 2:
Use the
AlarmManager
for the role of alerting the user at an interval greater than one minute (such as the requested 30 minutes)If the notification triggers an activity where you need to show updates is in the foreground, then also do something cheap, like
postDelayed()
, to give the user periodic updates in that activity