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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With