0

Please consider this chunk of code:

public class Presence implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
{
          :
          :
    boolean pref_auto_mode;
          :
          :
    private Presence(Context context)
    {
        this.context = context;
        getAppSettings();

        gApiClient = new GoogleApiClient.Builder(context, this, this)
                .addApi(LocationServices.API)
                .build();
        createLocationRequest();
        if (!gApiClient.isConnecting() && !gApiClient.isConnected())
        {
            gApiClient.connect();
        }
    } // of constructor

    @Override
    public void onConnected(Bundle connectionHint)
    {
        Log.e(LOG_TAG, "In onConnected(), gApiClient.isConnected(): " + gApiClient.isConnected());
        createLocationRequest();
        getLocation();
        getSubLocality();

        if (pref_auto_mode)
            startLocationUpdates();
    } // of onConnected()
          :
          :
    private void getAppSettings()
    {
        // Get the user's preferences
        APP_USER_SETTINGS =     PreferenceManager.getDefaultSharedPreferences(context);

                :        
                :        
        pref_auto_mode = APP_USER_SETTINGS.getBoolean(Contract.PREF_AUTO_MODE, false);
                :        
    } // of getAppSettings()

} // of class Presence

This is working as expected.

In the same Presence class above, I am trying to stop the location updates when the user switches a certain setting off, with the following method:

public void stopLocationUpdates()
{
    if (gApiClient.isConnected())
    {
        LocationServices.FusedLocationApi.removeLocationUpdates(gApiClient, pendingIntent);
        LocationServices.FusedLocationApi.removeLocationUpdates(gApiClient, this);

        Log.e(LOG_TAG, "Location tracker stopped successfully.");

        requestingLocationUpdates = false;
    }
}

I do see the Toast. There are no location updates and everything is silent as expected. But after a couple of hours of moving around with the device, all of a sudden, I start getting location updates again, as in, all the code in the onHandleIntent() of the IntentService get faithfully executed when there's a location change.

To set the Auto mode, the user has to launch the app, so the Presence class will certainly be instantiated (Singleton) and whatever is set in the app is reflected onto its pref_auto_mode flag, if I am not wrong. Yet, I suspect this flag.

Kindly let me know what else I need to do to bring the location updates to a grinding halt instantly and keep it that way till the user switches the setting ON? Am I doing something wrong or missing something?

Many thanks in advance!

Narayana J
  • 307
  • 5
  • 17
  • An intent service gets killed after it finishes doing its task so if you have any location manager in it, that too will get killed so you should probably move to a regular service for your location manager – tyczj Jun 15 '16 at 15:16
  • Thanks @tyczj, I *do not* have any `LocationManager` in the `IntentService`. I need the `IntentService` behavior very much, and its all working fine, except the *stopping* of the location updates... – Narayana J Jun 15 '16 at 15:24
  • what I am saying is that if you are trying to get location updates in an intent service its not going to work. please clarify if you are trying to do this and if not why are you trying to stop it in the intent service vs where you have the location updates running – tyczj Jun 15 '16 at 15:27
  • does your code look something like this? http://stackoverflow.com/questions/30141631/send-location-updates-to-intentservice also, show where in your code you request location updates.... – Daniel Nugent Jun 15 '16 at 15:57
  • Sorry, @tyczj, guess the information in the original question was not complete. Kindly see the updated question. To specifically answer your question, *NO*, I am not getting location updates in the `IntentService` and *NO*, I am not stopping the updates in the `IntentService` – Narayana J Jun 15 '16 at 16:02
  • Are you checking the user setting before the call to `FusedLocationApi.requestLocationUpdates()`? – Daniel Nugent Jun 15 '16 at 16:02
  • Yes, @DanielNugent, in the main activity's `onSharedPreferenceChanged()` listener, and in the main activity's `onResume()`. – Narayana J Jun 15 '16 at 16:09
  • Please allow me to be even more precise. Is the `stopLocationUpdates()` above, doing *all that it takes* to stop location updates, or is it missing somethings? Because I believe everything else is working as expected... – Narayana J Jun 15 '16 at 16:23
  • Show where you set/get/use the SharedPreference for this setting, and show your entire onConnected() override code where you request location updates – Daniel Nugent Jun 15 '16 at 17:42
  • @DanielNugent, sorry for supplying code only on-demand. Please review the updated question. Thanks! – Narayana J Jun 16 '16 at 10:26
  • 1
    Put a Toast inside the `if (pref_auto_mode)` case for testing, you will want to make sure it never gets in there when the setting is disabled – Daniel Nugent Jun 16 '16 at 14:53

1 Answers1

0

Thanks to all who reviewed the code and provided valuable pointers.

I had to make another boolean field variable stopLocationTrackerRequired and set it in the onSharedPreferenceChanged() override. Only if this flag is set, the Presence class's stopLocationUpdates() is invoked, and for sure turns off the location updates.

For completeness,:

  • Kindly let me know what else I need to do to bring the location updates to a grinding halt instantly and keep it that way till the user switches the setting ON?
  • Am I doing something wrong or missing something?
  • the stopLocationUpdates() is doing everything required to achieve the functionality.
  • I really wonder, but a little more control as shown above was lacking
Narayana J
  • 307
  • 5
  • 17