How to ensure a background process is running after device restart?

Hello,

I'm hoping to write code that, in an ideal situation, runs a background process every 5 minutes to gather basic metrics (steps, floors, cals) and POSTs it to an external database for some data analysis and dashboards.

I know how to do this for an app or a watch face, but I would much prefer the process to start without me starting an app (I can forget) or having my own watch face active,

Is there any way to achive this? Have a backround process that reliably starts after the device starts, without any user interaction?

Thanks

  • Ok, a background service is really part of your main app.  It's not really something separate that will run on it's own.  The main app has to run at least once after the app is installed and must do a registerForTemporalEvents.  If that doesn't happen, the background never runs.  But that only needs to happen once, and after that the backgrond will run when possible, even after you turn the watch off and back on.  (note "when possible" is something to keep in mind.  On some devices, if you are running a CIQ device app, none of the background services will run during that time - it has to do with available resources on the watch - mainly memory)

    Another thing is with a watch face, the background for it will only run if it's the "active" watch face - the one you see running on your watch.

    Your best option is a widget, Just moving through the widget or glance loop can be enough to do the registerForTemporalEvents, and again, only needs to happen once after the widget is installed.

    If you uninstall your app, the background service never runs again, so you need to keep it installed..

    Maybe a bit off topic, but you may not want the background to run every 5 minutes, but maybe a longer interval (15 minutes/30 minutes, etc) as the background does consume some battery, and there are times, like when you are asleep, that steps and floors won't change, or even if you are reading of watching TV.  In your case, you also want to handle cases where the makeWebRequest fails with an error

  • Great in-depth answer, thanks! I didn't know that the registerForTemporalEvents needs to run once and it won't unregister with device restarts, that's convenient.

    And fair enough, I'll look into the frequency of updates vs battery life.

    I get that the web request call can be power hungry, but does a backround process that "doesn't do much" still affect the battery life in a noticeble manner? Say I wanted to include a check that would only do the web request once the steps have changed significantly. Something like this:

    1. 1000 steps, initial upload, 1000 is stored in storage
    2. (5 minutes pass)
    3. 1200 steps, checks that the difference between current steps and stored steps is minimal, do nothing
    4. (5 minutes pass)
    5. 3000 steps, checks that the difference between current steps and stored steps is sufficient, make web request and update stored steps

    This way I could eliminate quite a lot of uploads at times where nothing is happening (like sleep).

  • Remember, even if something only uses a tiny bit of battery, if you run it 250 times a day, it can add up, more so than if it's run 25 times a day.

  • If the minApiLevel is 3.0.0 then can't the registerForTemporalEvents() be called from onAppInstall()?

  • I'm pretty sure the app still needs to be started once.  When you load an app and start it, you can see getServiceDelegate called a few times (in the background) onAppInstall, onAppUpdate, and then if there's a scheduled temporal event

  • Ok, I ran a quick test, where I created a really simple device-app that just has System.println() calls in onAppInstall and onAppUpdate.  Created a size load, copied it to the device and created a log file.

    Unplugged the device, let it get settled, and plugged it back in, and in the logfile, I see the message from onAppInstalled!

    Maybe I'll check onAppUpdate....

    -----------------------------------------------------

    Ok, same with onAppUpdate.

  • Interesting. I didn't think any of those would be called when side loading. 

  • Very helpful, guys, thanks a lot! I tried the onAppInstall and onAppUpdate on two different devices and it seems to be working as intended.

    (and yes, both side loaded)