Search code examples
javaandroidalarm

Android alarm repeated every (less then) 1 sec


I'm trying to check what app is in foreground and launch my app if user locked that app. To do so I fire alarm, start service to check for active app and set alarm to fire in one socound from now. But somehow the alarm is allways delayed to 5 secounds..

This is how Im setting it:

alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, alarmIntent);

I've tried it on android 5.1 and also on 6.0. It's the same.

Do you know why it is delayed? or do you know about any better way to check for active app as soon as possible? because I think this way consume lot of battery.

Thanks.


Solution

  • So this is what I was looking for. Only disadvantage is that user has to enable accessibility service manualy from settings.

    https://stackoverflow.com/a/27642535/4506191