Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPS OnPause code does not turn off the GPS

Tags:

android

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.

like image 820
Allen Edwards Avatar asked Nov 04 '22 20:11

Allen Edwards


1 Answers

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);
like image 105
ρяσѕρєя K Avatar answered Nov 14 '22 23:11

ρяσѕρєя K