Enabling/Disabling Position.enableLocationEvents in App settings

Sorry for this basic questions, but I am not sure how to enabling/disabling LocationEvents in a running application. 

I have an app which enabled LocationEvents based on a parameter in properties.xml.

function onStart(state) {
        if (Application.Properties.getValue("gpsmode_prop") == true ) {
            Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
        }
}

function onStop(state) {
        if (Application.Properties.getValue("gpsmode_prop") == true ) {
            Position.enableLocationEvents(Position.LOCATION_DISABLE, method(:onPosition));
        }
}


While starting up the App with gpsmode_prop == true||false everything works well and GPS is either on or off. However, if I want to switch it off or on, while the App is running, I would need to call Position.enableLocationEvents(Position.LOCATION_DISABLE, method(:onPosition) or Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition)); from the MenuDelegate. 

if (gpsmode == true ) {
    Application.Properties.setValue("gpsmode_prop", false );
    Position.enableLocationEvents(Position.LOCATION_DISABLE, null);
} else {
    pplication.Properties.setValue("gpsmode_prop", true );
    //Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
}

Disabling works as well, as I can set method to null, but I am actually not sure how to link back to the Application.AppBase to call the onPostion method.

  • One approach is to use Application.getApp() to get a reference to your app class that extends AppBase.

    e.g.

    class GpsApp extends Application.AppBase {
        // ...
        function onStart(state) {
            if (Application.Properties.getValue("gpsmode_prop") == true) {
                enableGps();
            }
        }
    
        function onStop(state) {
            if (Application.Properties.getValue("gpsmode_prop") == true) {
                disableGps();
            }
        }
    
        function onPosition() {
            //...
        }
    
        function enableGps() {
            Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
        }
    
        function disableGps() {
            Position.enableLocationEvents(Position.LOCATION_DISABLE, null);
        }
    }
    
    //...
    class GpsSettingsMenuDelegate /* ... */ {
        // ...
        function toggleGps() {
            var app = Application.getApp();
            if (gpsmode == true) {
                Application.Properties.setValue("gpsmode_prop", false);
                app.disableGps();
            } else {
                Application.Properties.setValue("gpsmode_prop", true);
                app.enableGps();
           }
        }
    }

    A similar approach would be to pass a reference to your app class to the main view, which would pass it to the menu delegate, but getApp() is a bit simpler.

  • Whow, thanks a lot. Grinning That was a great learning experience. Functionality wise, many functions work as expected, however, still a lot of work ahead to optimise the code. Still a bit surprised about that. Additionally to that I also added a "new" class to draw the GPS status indicator in different colours, which allows me to resize, recolor or hide it based on GPS quality, GPS setting or Display size. Thumbsup