How to Switch ON and OFF GPS

1.7k views Asked by At

This question has been asked many a times and i have found many results but none of them really helped me.

Using the below code to Switch ON GPS

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.i("GPS IS ON", "GPS IS ON"+provider.contains("gps"));           

if(!provider.contains("gps")){ //if gps is disabled

        Log.i("GPS IS ON", "GPS IS ON");

        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);
    }

and to switch OFF :

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

        if(provider.contains("gps")){ //if gps is enabled
            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);
        }

i am calling these 2 different methods on 2 different clicks.It should automatically switch on the GPS on the device but its not working.

i have even added these permissions :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I have tested these in 4.4 and 4.3 and does on Switch on the GPS automatically.

Also LOCATION_PROVIDERS_ALLOWED is Deprecated in 4.4 so what is the alternative for that ?

EDIT

LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new MyLocationListener();

        try {
            gps_enabled = locManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
        try {
            network_enabled = locManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        // don't start listeners if no provider is enabled
        // if(!gps_enabled && !network_enabled)
        // return false;

        if (gps_enabled) {
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    0, locListener);

        }

        if (gps_enabled) {
            location = locManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        }

        if (network_enabled && location == null) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    0, 0, locListener);

        }

        if (location != null) {

            MyLat = location.getLatitude();
            MyLong = location.getLongitude();

}

I have had this code working but seems that if internet is Not there it wont work with GPS turned OFF.

2

There are 2 answers

0
Prathmesh Swaroop On

I think one cannot change the user's personal phone settings(Like GPS turn on-off, sound on-off,display etc.) from any application. It would be security voilation.

In my opinion here only option is to check if GPS is on or off and Redirect User to Settings screen.

LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
        showPopupToRedirectToSettings();
    }

By using following Intent you can launch settings screen:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
1
vadher bhargav On

Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)