0

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.

Alex Mensak
  • 158
  • 1
  • 2
  • 14
  • 2
    "Do you know why it is delayed?" -- probably because you are asking for it too frequently. `setRepeating()` has a minimum of a one-minute period on Android 5.1+ for much the same reason. "do you know about any better way to check for active app" -- that is not supposed to be possible anymore, for privacy reasons. – CommonsWare Jan 13 '16 at 19:46
  • So there is no way of scheduling service every 1 second? – Alex Mensak Jan 13 '16 at 19:52
  • 1
    Any way of doing work every 1 second will "consume lot of battery". – CommonsWare Jan 13 '16 at 19:56

1 Answers1

0

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

Community
  • 1
  • 1
Alex Mensak
  • 158
  • 1
  • 2
  • 14