Need help with Background process

Need to arrange background process, which will be called for every round hour (min and sec = 0), get data from API, store it in arrey in property or base and exit bg process.

Main question, how to call bg process for requestd time momen even if app inactive.

Thsnks for ideas)))
  • thanks alot, very usefull manual!))
    All working good now)

    Coild you advise,
    for temporalEvent i've used duration 3600, to call it every hour. And i made this call same as in your sampe in initialise function of view. As it should, it's take 1 hour from moment of view initialise....

    Any idea hot to make it start for every round hour (min and sec are 0), whayever time WF will stats?
  • Use a moment and reschedule as soon as the previous one ended. (Instead of using duration).
    Check documentation in SDK to see difference between the 2 ways of registering.
  • Dymitri -
    You can't actually be sure it always runs at 0 mins and 0 seconds, as the background may be blocked from running due to memory, and may not run until 20m and 33 seconds or something. It may not even run for that hour. The background may also run about every hour, but the watchface itself might not run during that hour, so you may want to look at using getBackgroundData() in the background process, and just add new data to what's already queued up for the watchface.
  • thanks for replys) will try to reschedule in backgroundData)
  • Hermo's post is the best way to get it to try at 0min, 0sec. But take into account that it might not run until after that. Maybe consider a timestamp in what you return with Background.exit() so you can tell when it's from, and maybe do something different in the watchface

    I have a widget that returns pressure data, and the way I did it, is I allow for up to 48 values, both in the widget and background. Based on the length of the graph I display, that means 12 values/hour for a 4hr graph, and 2 per hour for a 24 hour graph. The total time for the graph is actually determined by how often I have the background run, so for a 4hr graph, every 5 minutes, and the 24 hr graph, every 30 minutes. With a timestamp in each reading, I can see if there are any "misses", and also easily tell the length of time the graphs have data for, with the timestamp of the newest and oldest values
  • Thaks alot )))
    All working greatly!))
    I've done time momet for frst run in view - initialise:
    3600- Time.now().value() - (Time.now().value()/3600)*3600;
    will geave seconds to closest round hour.
    And for same formula moment re-initialise at backgroundData)))

    Now have question about buttons)))
    The idea is:
    need on press of "back" button to switch flag between 1and 0. Flag can be stored anywhere: global var, property or app.base, not so important.
    From result of the flag in view section will be run one from two function (on flag 1 and flag 2).

    Api and develope's guides did not help tu understund, how to mke it...
  • In a watch face, you can't do any input (buttons, swipes, etc). If you want to have different options, you can use app-setting to change it from a phone or Garmin express, or maybe do something like change (rotate) the option every minute, or maybe have one when your in low power and one where you aren't,
  • Thaks alot )))
    All working greatly!))
    I've done time momet for frst run in view - initialise:
    3600- Time.now().value() - (Time.now().value()/3600)*3600;
    will geave seconds to closest round hour.
    And for same formula moment re-initialise at backgroundData)))

    Brilliant!

    Thanks to Jim for further assisting in my absence!
  • Hermo, Thanks!

    Dmytri, he's the basics of how I do pressure data in the background.. No sensor history involved, and I do both ambient and MSL pressure. This will only work with 2.x devices with a baro altimeter.
    function onTemporalEvent() {
    var old=Background.getBackgroundData();

    var actInfo=Activity.getActivityInfo();

    var data=[[Time.now().value(),actInfo.ambientPressure,actInfo.meanSeaLevelPressure]];
    //at this point, merge "old" with "data", so readings aren't lost in the main app, and in a way the main app can use and understand it it. This is also where I limit what's returned to the 48 samples I mentioned before
    Background.exit(data);
    }