1

We are busy developing an app that has the following requirements,

  1. Track the user's location throughout the day so that we can plot his/her route on a map
  2. The app should store all information locally on the device and when the device is able to connect to the server, it should auto-sync its data.

For this, we are trying to use the Background Location and Background Fetch functions. We created a proof of concept app that does the following,

  1. Location Tracking: The location listener simply writes its coordinates to a SQLite database on the device for later use.

The setting of the Background Listener

        LocationManager locMan = LocationManager.getLocationManager();

    if (locMan == null) {
        Dialog.show("Location Error", "Location Manager not found", "OK", null);
    } else {
        if (!locMan.isGPSDetectionSupported()) {
            Dialog.show("Location Error", "GPS detection not supported.", "OK", null);
        } else {
            if (!locMan.isGPSEnabled()) {
                Dialog.show("Location Error", "GPS not enabled. Please close the app, turn on your location and try again.", "OK", null);
            } else {
                if (locMan.isBackgroundLocationSupported()) {
                    locMan.setBackgroundLocationListener(LocationListener.class);
                    Dialog.show("Location", "Background location listener loaded.", "OK", null);
                } else {
                    Dialog.show("Location Error", "Background location not supported", "OK", null);
                }
            }
        }
    }

And the the Listener Class itself,

public class LocationListener implements com.codename1.location.LocationListener {

@Override
public void locationUpdated(Location arg0) {
    String msg = arg0.getLatitude() + " - " + arg0.getLongitude();

    ASDTester.logMsg(ASDTester.MSG_LOC_COORDS, msg);
}

@Override
public void providerStateChanged(int arg0) {
    String msg = "" + arg0;

    ASDTester.logMsg(ASDTester.MSG_LOC_STATE_CHANGED, msg);
}

}

The call to ASDTester.logMsg(int type, String msg) simply creates a record in the SQLite database table

  1. Background Fetch: The performBackgroundFetch implementation also logs a message to the SQLite DB storing the timestamp of when the method fired,

Setting Background Fetch Interval,

if (Display.getInstance().isBackgroundFetchSupported()) {
        Display.getInstance().setPreferredBackgroundFetchInterval(60);
        Dialog.show("Background", "Background Fetch Interval set", "OK", null);
    } else {
        Dialog.show("Background Error", "Background Fetch Not Supported", "OK", null);
    }

Implementing performBackgroundFetch

    @Override
    public void performBackgroundFetch(long arg0, Callback<Boolean> arg1) {
       logMsg(MSG_BGF_TIMESTAMP, System.currentTimeMillis() + "");

        arg1.onSucess(Boolean.TRUE);
}

And then finally the Build Hints for iOS,

ios.background_modes=fetch,location

The implementation does not work on Android or iOS. The location listener only fires once when it is initialized. And the Background fetch does not fire at all.

  1. Are we doing something wrong in our implantation of these functions?
  2. How would one go about debugging these functions to try and see what is going wrong?
SwissLoop
  • 466
  • 2
  • 11
  • On iOS did you add the NS description strings to indicate why you need background support? Android is a bit rough with all of these and breaks with every update. It's important to look at the version of Android your testing on. Check with a cable connected and look through the native logs to see if there are important printouts – Shai Almog Aug 19 '20 at 05:54

0 Answers0