简体   繁体   中英

Garmin makeWebRequest in background service delegate can not wake app from callback

Trying to request a webrequest in the background, and trigger an application wake when it finishes. The example code works, but it's impossible to wake the app from a callback:

using Toybox.Background;
using Toybox.Communications;
using Toybox.System;

(:background)
class BackgroundService extends System.ServiceDelegate {

    function onTemporalEvent() {       
        Background.requestApplicationWake("do you want to open the app?"); 
        Background.exit(null);
    }
}

This does not work:

using Toybox.Background;
using Toybox.Communications;
using Toybox.System;

(:background)
class BackgroundService extends System.ServiceDelegate {

    function onTemporalEvent() {
        Communications.makeWebRequest(
            "https://jsonplaceholder.typicode.com/todos/1",
            {},
            {},
            method(:responseCallback)
        );
    }

    function responseCallback(responseCode, data) {
        Background.requestApplicationWake("do you want to open the app?");       
        Background.exit(null);
    }
}

I had this same issue. I posted an answer on the Garmin forum, but I'll post here too, just in case.

Give this a try:

using Toybox.Background;
using Toybox.Communications;
using Toybox.System;

(:background)
class BackgroundService extends System.ServiceDelegate {

function onTemporalEvent() {
    Communications.makeWebRequest(
        "https://jsonplaceholder.typicode.com/todos/1",
        {},
        {},
        method(:responseCallback)
    );
    Background.exit(null);
}

function responseCallback(responseCode, data) {
    Background.requestApplicationWake("do you want to open the app?");       

}

}

For me, this a great solution, because I don't implement onBackgroundData.

For those of you who need background data from the web request callback, I tried seeing if you can call Background.exit twice (once right after the web request and once in the callback), but I was running into issues with onBackgroundData (in general) and I don't have anymore time to investigate. So, I'll leave that task to someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM