0

In my Android app, I need a certain bit of code to execute every minute, whether the phone is active or not.

(For those curious, the app is meant for a personal project, a "talking" clock which will need to check every minute if that time has a corresponding sound file to play. It's not something I plan to release to the world, so battery considerations are not in play.)

My current approach is to use Timer.scheduleAtFixedRate() to schedule a task.

This seems to work whenever I am looking at the app, and interacting with it occasionally to keep the screen from blanking, but if the phone turns the screen off to save power, it seems like my call happens sporadically.

I tried setting the interval to be every 30 seconds, but even then it seems like I miss some minutes. Are there specific considerations to using Timer on Android? Is there a better way to achieve what I need?

levik
  • 114,835
  • 27
  • 73
  • 90

2 Answers2

2

Question: are you 100% absolutely sure you need to be doing this every minute? It just sounds to me that you'll be hogging the battery like crazy and will get quite a few unhappy users.

But if you answer yes to that question: After your activity is paused, there's not guarantee from the system that anything on it (including your task) will be kept running; that way as soon as the systems needs a couple of megabytes to do anything it will kill your activity and stop your timer.

You should implement the timer/task in a Service. Services are much less likely to be killed by the system and you might ask that if the system needs to kill it to re-created it as soon as possible.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Hip Hip Away is also a valid alternative, but I've never worked with it. – Budius Aug 15 '12 at 14:55
  • +1 because of your question about it being needed every minute – ChrisHarris2012 Aug 15 '12 at 15:03
  • Added the description of my project to the question. Is there anything I can do in onPase/onStop to prevent the system from killing my Timer? – levik Aug 15 '12 at 15:13
  • 1
    no. Activity are not meant for long running process. Considering that it's a personal/test project you can simply mark on the XML layout to `KeepScreenOn = true` and leave it running. But after its onPause, the system takes over and will do as it's programmed to do. – Budius Aug 15 '12 at 15:15
  • 1
    just as an example: https://play.google.com/store/apps/details?id=com.sportstracklive.stopwatch it's a timer running as well. But I'm 100% sure it implements a service on the background to keep track of time and update the notification and when the activity comes to the foreground it binds to this service. Makes sense? – Budius Aug 15 '12 at 15:19
1

Have you tried using AlarmManager, this will let you do a task every X amount of time even if the phone is in standby mode or off

Here are the docs for it

If you want a nice example of using an AlarmManager, here it is... This one does not work if the phone is turned off but you can enable this easily if you want

Community
  • 1
  • 1
Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80