5

GPS will not stop even thouth I have installed OnPause code to stop the GPS service. Here is my code.

    @Override
    protected void onPause() {
        if(lm != null) {
            lm.removeUpdates(ll);
        }
        ll = null;
        lm = null;
        super.onPause();
    }

This code runs without errors as long as lm and ll are declared as protected variables globally. The problem is that the GPS icon stays on after I leave the program. How do I turn GPS off? I have tested this on a phone and the emulator.

Allen Edwards
  • 1,488
  • 1
  • 27
  • 44

3 Answers3

1

as doc says about removeUpdates(LocationListener listener) :

Removes any current registration for location updates of the current activity with the given LocationListener.

Your Current Code only Removes any current registration for location updates of the current activity with the given LocationListener not STOP GPS or remove icon from statusbar .so is you want to stop GPS then you have two solution

FIRST SOLUTION : stop by code (only work on below Android 2.3):

  try{
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(!provider.contains("gps")){
         final Intent poke = new Intent();
         poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
         poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
         poke.setData(Uri.parse("3")); 
         sendBroadcast(poke);
     }
     }
     catch(Exception e)
     {
      Log.d("Location", " exception thrown in enabling GPS "+e);
     }

Permission in Manifest.xml:

android.permission.WRITE_SECURE_SETTINGS

SECOND SOLUTION : launch Gps setting Activity:

startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • My phone is Android 4. Second solution allows user to disable GPS, not what I want to do. I want to turn it off but not disable it. I know it is possible because iRegatta does it. – Allen Edwards Jul 10 '12 at 07:47
  • You said that solution 1 would not work on my phone, correct? I understand it is exploiting a bug that has been fixed on later phones. Correct me if I misunderstood. Solution 2 isn't what I want. I want the GPS to go off when you exit the app. What I got with your second solution was the screen to disable GPS. I do not want to disable it. I want it to go off without user doing anything just like it does in iRegatta. – Allen Edwards Jul 10 '12 at 07:57
  • @AllenEdwards : i understood what you want and i'm trying to help you. can you tell me if GPS is diable on device then how you are starting GPS? – ρяσѕρєя K Jul 10 '12 at 07:59
  • GPS is enabled all the time but only goes on when a program uses it. It goes on when I open my app which sets up a location manager and location listener. It goes off after an hour or so after exiting the app. With iRegatta, it goes off as soon as the app is exited with the exit button I might add, not with just back arrow out of the app. – Allen Edwards Jul 10 '12 at 08:10
1

My problem was that the location manager and location listener names were being redefined in the on change location code so that they were not the global ones referenced in the global statements. I copied this code from a YouTube tutorial and I would assume many others have this same problem. Once I deleted the re-definition not the GPS goes off almost instantly when the screen is back arrowed out. In fact, the GPS goes off so quickly, this is now a problem I need to deal with.

See lm.removeUpdate(ll); not releasing updates

Community
  • 1
  • 1
Allen Edwards
  • 1,488
  • 1
  • 27
  • 44
  • This helped me too - turns out I was setting up multiple listeners but my removeUpdate() code was only removing one of them. – Carl Anderson Mar 25 '14 at 22:53
0

Use this in your onPause()

protected void onPause() 
{
    super.onPause();
    mylocaationoverlay.disableCompass();
    removeLocationUpdates();
}

This will be your removeLocationUpdates()

public void removeLocationUpdates()
{
    if(locationManager_ != null && myLocationListener_ != null)
        locationManager_.removeUpdates(myLocationListener_);
}

Or try this: onPause to stop LocationManager

Community
  • 1
  • 1
user1351659
  • 54
  • 1
  • 9