In Background

Is there a way to tell if I'm in the Background Process?

I know about the method of setting an "inBG" flag in "getServiceDelegate". That is clever. But is there a "has" I can call or something else that I can use inside onStart() to let me know if this is executing in the BG or FG? Maybe checking total memory, which is MUCH less in the background? Hmm that works! If BG always has less than 32K I'm good.

  • I found this code in one of my old apps. I played with the idea a year ago but it wasn't implemented at the end, but I think it should work:

    (:no_ciq_2_4_0, :background)
    function isRunningInBackground() as Boolean {
        try {
            var app = getApp();
            app.setProperty(PROPERTY_NON_EXSISTANT, 1);
            app.deleteProperty(PROPERTY_NON_EXSISTANT);
            return false;
        } catch (ex) {
            return true;
        }
    }
    (:ciq_2_4_0, :background)
    function isRunningInBackground() as Boolean {
        try {
            Properties.setValue(PROPERTY_EXSISTANT, false);
            return false;
        } catch (ex) {
            return true;
        }
    }
    

  • There might be a much easier, smaller,more elegant way. Haven't tried it yet, it's based on Flow state's code in another thread:

    For sure you have some foreground only class, variable or method. Let's call it $.firegroundOnly. You can check: $ has :foregroundOnly

  •  unfortunately this method doesn't work, at least not in SDK 8.1.1 in the simulator of fr955:

    I have a file called BackgroundUtils.mc, with 2 global functions in it:

    import Toybox.Background;
    import Toybox.Lang;
    import Toybox.Time;

    (:background)
    function isRunningInForeground() as Boolean {
      return $ has :triggerTemporalEvent;
    }

    (:foreground) // not needed, just for extra clarity
    function triggerTemporalEvent() as Moment {
      // this is not the real code, I'm shortening it for the sake of the forum post
      Background.registerForTemporalEvent(Time.now());
      return moment;
    }

    And isRunningInForeground always returns true in the Background process, even when compiling with --disable-api-has-check-removal.

    I even tried it with a global class (my View's class, that obviously is not loaded in the background), with the same result.

    To me it looks like a bug in the SDK, what do you think?

  • And isRunningInForeground always returns true in the Background process, even when compiling with --disable-api-has-check-removal.

    We talked about this before, but I'm not sure why you think --disable-api-has-check-removal does anything other than exactly what it says [disable has check removal for API symbols]. I don't think it disables replacing enums with their values [to use the example that came up in the other discussion], and if there was some other optimization for removing general has checks [not sure if there is], I don't think it would disable that optimization either.

    The correct test (imo) would be to disable optimization.

    Anyway, I tried your approach with and without optimization, and:

    - Indeed, $ has :triggerTemporalEvent is true for both the bg and fg process

    - if I actually call triggerTemporalEvent [I used a different version which just logs a message to the console]:

    -- it works in the FG (as expected)

    -- in the background I get the following at the line which calls triggerTemporalEvent:

    Error: Illegal Access (Out of Bounds)
    Details: Failed invoking <symbol>

    (Ofc, if I increase the type check level to 2 or 3, I do get a warning about triggerTemporalEvent not being available in all function scopes, so that part works)

    I think this kind of makes sense. There aren't two copies of the global module (for example) - one for BG and one for FG - there's just a single copy which has all the symbols.

    I realize this approach was my idea in the past haha, but I don't remember if it was ever tested properly. Perhaps something has changed since then? Either that or it was just another idea that sounded great in theory haha

  • Tested now, it didn't work in SDK 7.4.3 either

  • I found the same behaviour in 7.4.3 and 6.4.2 (testing with fr935 in the sim).

    Sorry for the bad idea :/

  • Actually it was a good idea ;) it's a pity it doesn't work.