Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle GPS Programmatically Android 4.4

Tags:

android

gps

root

I know. Don't do this. I don't care. It's for a root app.

The app is installed to /system/app/ with 0777 permission

I was previously using:

ContentResolver cr = context.getContentResolver();
Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, !isGpsOn);

This is how I'm trying it on 4.4 since that was deprecated:

int value;
if (isGpsOn)value = Settings.Secure.LOCATION_MODE_OFF;
        else value = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
Settings.Secure.putInt(cr, Settings.Secure.LOCATION_MODE, value);

And it is silently failing (according to some user reports). How can I properly toggle GPS with Android 4.4 from an app in System folder?

like image 558
Reed Avatar asked Apr 05 '14 22:04

Reed


People also ask

How do I turn on GPS automatically on Android?

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.

How can I enable or disable the GPS programmatically on Android?

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 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.

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.


1 Answers

Don't do this. :)

Check the code for GPS TOGGLER

GPS toggle widget for Android rooted devices. It wrks well even for those ROMs and kernels, other software failed.

With your code, just checking:
Hope you declared the following permissions in your manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

I had used this on rooted LG Optimus, android 4.0/4.1. Reportedly this may no longer work but i am not sure about it for all devices and o.s.:

Switch it ON/OFF with a flag check and this code:

try {  
  Settings.Secure.putString (context.getContentResolver(),  
  Settings.Secure.LOCATION_PROVIDERS_ALLOWED, String.format ("%s,%s",  
  Settings.Secure.getString (context.getContentResolver(),  
  Settings.Secure.LOCATION_PROVIDERS_ALLOWED), LocationManager.GPS_PROVIDER));  
        } catch(Exception e) {}
like image 152
Pararth Avatar answered Oct 22 '22 10:10

Pararth