Android Background Service

I've been trying to get my head around this and struggling for days! I have a countdown timer running in my app that shows an alert when the time is up.

The problem is when the app goes to sleep or the device is shut down.

My timer code is situated in app.js which fires an event to update the label text in another file. I have managed to get this working on IoS using background services that re kickstarts the timer - but having no joy with Android.

Ideally I'd like to get the timer running again so when the user re-opens the app the timer text is correct, but struggling with this.

I've tried to play around with services and intents, but I am really struggling to understand how this works. Is it possible to create something that will ensure the timer text is correct when the user re-opens the app; or failign this create an alert at the very least when the timer is finished?

Any help, as always, is massively appreciated. I've read the wiki guide on simple services and explored the kitchen sink, but I'm not even sure if that's the way I need to go as I don't really understand the concept that well.

2 Answers

  • Check out the wiki for Scheduling Future Notifications. This has a complete example for Android.

    — answered 4 years ago by Adam Paxton
    • Hi Adam, thanks for the response. I'm slowly getting my head around this, but could you perhaps confirm one thing for me.

      I can schedule a notification to occur when the timer is due to be finished using the above guide, but it seems whenever the app goes to sleep/phone turned off that my setInterval timer is paused.

      As a result, when the user switches the app back on the timer text is completely out of sync - is there anyway to keep this timer maintained, as it's a little misleading. For example, the user puts a 10 minute timer on, the phone hibernates after several minutes, yet when the user switches back to the app the time displayed is relative to when the app goes to sleep because the timer appears to pause.

      Is there anyway around this - otherwise I guess my plan B is to remove the timer text if the app sleeps and just rely on the future notification?

      Many thanks for all of your help…

      — commented 4 years ago by Chris Leyton
    • I guess to summarise my dilemma - is it even possible to keep a timer running in the background with Android?

      — commented 4 years ago by Chris Leyton
    • Hello Chris, I think that when Android apps go in to background, you don't have a guarantee that things will continue to run as expected, as the OS can stop it when it needs resources, etc.

      I think your best bet is to just schedule the notification(s) for the time you want them to run instead of using the setInterval, and if you only want it to show when in background, then look in tracking when the app goes in to background using the pause or resume events. Or, you could use the setInterval, and when the app goes to background, use the pause event to schedule the notification with the time remaining in your timer. I"m just not sure how much android will process in the pause event since the app is being paused, but you might give it a try.

      — commented 4 years ago by Adam Paxton
  • Android Timer (even when the phone goes to sleep)

    When the phone goes to sleep, the app is “paused” and therefore, the timer is paused. To solve this issue on Android, I added the WAKE_LOCK permission to tiapp.xml:

        <android xmlns:android="">
            <manifest android:versionName="1.0.0">
                <uses-permission android:name="android.permission.WAKE_LOCK" />
                <service type="interval" url="myservice.js"/>

    I am using an interval service that updates a progress bar; for an example, please look at ‘examples/android_services.js’ in the Kitchen Sink.

    Iphone Timer

    I use the ildeTimerDisabled

    Ti.App.idleTimerDisabled = true; // to disable going to sleep (iPhone only) 
    Ti.App.idleTimerDisabled = false; // to re-enable it

    Hope this helps.