Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch off GPS programmatically

Tags:

android

gps

What i have: Currently my app is giving location through gps.

what i want: Gps to turn off automatically after i exit from the application. Because it keeps on telling me the location time and again that looks odd and also gps consume a lot battery.

like image 767
Noman Avatar asked Aug 03 '11 11:08

Noman


People also ask

How do I turn off GPS when not in use?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

What is enable or disable GPS in android programmatically?

Programmatically we can turn on GPS in two ways. First, redirect the user to location settings of a device (by code) or another way is to ask to turn on GPS by GPS dialog using LocationSettingsRequest and SettingsClient.

How do you fake your location programmatically and avoid being tracked by companies?

“Mock Location” is a hidden developer setting in the Android operating system that allows a device owner to set any GPS location for testing purposes. Despite the fact that this is a developer setting, anyone can use it just by clicking a couple of buttons in device settings.

How do I open location settings in android programmatically?

Get current location settingsTask<LocationSettingsResponse> task = client. checkLocationSettings(builder. build()); When the Task completes, your app can check the location settings by looking at the status code from the LocationSettingsResponse object.


2 Answers

Looking at above comment thread it seems its possible to turn OFF GPS programatically (But seeing only 12 Upvotes)

But if you switch OFF GPS from your application programatically, what if other applications use GPS Service ?

The solution would be like

Open Settings of android and then tell user to turn OFF GPS when he exits from the application...

For this you can do like :

Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);

OR

You can try like

locationManager.removeUpdates(myLocationListener); 
locationManager = null;

This shutdown gps for this app, but its still available for use by other apps.

like image 117
Kartik Domadiya Avatar answered Sep 25 '22 13:09

Kartik Domadiya


This code can alter your gps.But it is not documented

 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);
like image 44
Rasel Avatar answered Sep 25 '22 13:09

Rasel