3

I would like to hear a beep every 10 seconds, 7*24 no matter what. What should I do?

  1. it works on Doze mode.
  2. it works exact time.
  3. it works w/o GCM.
  4. it works on custom ROM.

If it is not possible, would it become doable changing the 10 seconds to 1 min, and dropping the exact time?

Remark: regarding item 4. custom ROM, many manufacturer kill the background running like AlarmManager in order to save the battery.

qcm217
  • 143
  • 9
  • I would totally secretly install such app to one of my colleague's phone. Ping me when its available :) P.S. it would be even better if you cannot uninstall it! – Pavel Dudka Sep 18 '16 at 05:48
  • haha. Good to know it is not available, but could you tell me which conditions are not available please? – qcm217 Sep 18 '16 at 06:12
  • oh, sorry, I didn't mean functionality you are looking for is not available :) Just want an app. Answer is posted separately – Pavel Dudka Sep 18 '16 at 07:19

2 Answers2

1

I'm pretty sure AlarmManager is a way to go. Alarm is not a background work - it is a scheduled piece of work which will run at specified (exact) time in the future. If, as you say, manufacturers would kill those to preserve battery it would mean that user's alarms (like 8am wake up time) will not work anymore which doesn't make sense.

Please note that behavior of AlarmManager has been changed in API 19 (and above), so by default Alarms fire off time is inexact. This was done for battery optimization reasons. However, it is still possible to ask for an exact delivery time by using AlarmManager#setExact or less AlarmManager#setWindow APIs.

Older platforms will behave as before when all alarms by default are exact

More information on AlarmManager and some code samples you can find here

Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • 1. the alarmmanager min interval is 15min in exact time. 2 about the alarmmanager get killed by manufacturers, the mechanism I don't know, but I guess it is a rooted system which would force stop or disable apps when the system is idle. See this link http://stackoverflow.com/questions/34729966/alarmmanager-not-working-in-several-devices – qcm217 Sep 18 '16 at 07:44
0

You need to use Android Background Service. and CountDownTimer. check links for reference.

SService will work in background mode. and you can set interval in CountDownTimer of 10 seconds.

Learn Pain Less
  • 2,274
  • 1
  • 17
  • 24
  • I know AlarmManager and Service. But I think this post accent 10 seconds, 7*24 no matter what. – qcm217 Sep 18 '16 at 05:45
  • yes you can make a method to run CountDownTimer and call that method inside same method. (so it will work in loop) – Learn Pain Less Sep 18 '16 at 05:46
  • I don't like the rush reply, it ruins the post making people think the problem had answer. AlarmManager cannot even satisfy the every 10 seconds.(min interval is 1 min) – qcm217 Sep 18 '16 at 05:48
  • you can check this post for more info http://stackoverflow.com/questions/10221996/how-do-i-repeat-a-method-every-10-minutes-after-a-button-press-and-end-it-on-ano – Learn Pain Less Sep 18 '16 at 05:51
  • I don't think my post can be solve by a simple loop, the troublesome is when the phone is locked/sleep, all backgrounds get killed. – qcm217 Sep 18 '16 at 05:53
  • http://stackoverflow.com/questions/8713361/keep-a-service-running-even-when-phone-is-asleep – Learn Pain Less Sep 18 '16 at 05:54
  • I remember I personally tested the min interval on Android6.0 is 1min... – qcm217 Sep 18 '16 at 06:01
  • but i m using 3 second interval on Android 6.0.1 – Learn Pain Less Sep 18 '16 at 06:12
  • maybe different interval in different method, the 1 min interval I tested is setInexactRepeating method. – qcm217 Sep 18 '16 at 06:28