10

Is it possible to turn on LocationProviders(GPS Provider/Network Providers) on Device programatically, if it is turned off?

RandomMooCow
  • 734
  • 9
  • 23
Prem Singh Bist
  • 1,273
  • 5
  • 22
  • 37

6 Answers6

28

No, it is not, but you can open the Location services settings window:

context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
Rotemmiz
  • 7,933
  • 3
  • 36
  • 36
7

Enabling Location with mode High Accuracy or Battery saving without user needing to visit Settings

https://android-developers.googleblog.com/2015/03/google-play-services-70-places-everyone.html

ABN
  • 1,024
  • 13
  • 26
Akhil Dad
  • 1,804
  • 22
  • 35
  • 1
    As of Google Play Services 7.0, this should be the accepted answer. You CAN have the app update settings without ever leaving the app. See link above. – Charlie Dalsass Jun 29 '15 at 14:33
  • 2
    The link doesn't says much about implementation, did I missed something ? – Philippe David Jul 16 '15 at 13:19
  • Implementation example is here: http://stackoverflow.com/questions/33251373/turn-on-location-services-without-navigating-to-settings-page – MorZa Aug 14 '16 at 11:39
  • With that said, this does not turn on the location service or specific providers programatically as asked. It shows the user a dialog with an option to turn the location service on. – MikeL Sep 21 '16 at 08:11
  • @MikeL Yes you can not turn on locations automatically without user intervention as it costs data and battery to user – Akhil Dad Sep 23 '16 at 09:59
  • There are a lot of comments here preaching about the user consent and developer integrity with regards to getting user location but you can do far more "damage" to user device in terms of battery drain or stealing data using other much simpler methods with or without consent from the user. On the other hand I can think of a lot of apps where accessing user location without his consent at that specific moment should be a must feature, like anti theft apps. You can ofcourse argue, and I agree, that you MUST notify user (or ask for his permission) to do such a thing on the first launch of the app – MikeL Sep 24 '16 at 09:37
3
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

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

But this code support only upto APK 8.

mob_web_dev
  • 2,342
  • 1
  • 20
  • 42
1

Yes but you have to be a Super User (sudo), Your device must be rooted.

Kishore
  • 952
  • 2
  • 11
  • 31
1

With recent Marshmallow update, even when the Location setting is turned on, your app will require to explicitly ask for permission. The recommended way to do this is to show the Permissions section of your app wherein the user can toggle the permission as required. The code snippet for doing this is as below:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
    if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Location Permission");
        builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
        builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
    
            }
        });
        builder.setNegativeButton(android.R.string.no, null);
        builder.show();
    }
} else {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean isGpsProviderEnabled, isNetworkProviderEnabled;
    isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if(!isGpsProviderEnabled && !isNetworkProviderEnabled) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Location Permission");
        builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
        builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        builder.setNegativeButton(android.R.string.no, null);
        builder.show();
    }
}

And override the onRequestPermissionsResult method as below:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_COARSE_LOCATION: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "coarse location permission granted");
            } else {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }
        }
    }
}

Another approach is you can also use the SettingsApi to inquire which location provider(s) are enabled. If none is enabled, you can prompt a dialog to change the setting from within the app.

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
-1

Use the following code to start GPS

locationManager = ( LocationManager ) getSystemService ( Context.LOCATION_SERVICE );
locationListener = new LocationListener();
locationManager.requestLocationUpdates ( LocationManager.GPS_PROVIDER, 0, 0, locationListener );

and the following code to stop GPS

locationManager.removeUpdates( locationListener );

For further details see the documentations for LocationManager and for LocationListener.

Conspicuous Compiler
  • 6,403
  • 1
  • 40
  • 52
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • 1
    i am talking about turning on (Settings-->Location & Security -->Use wireless Networks/Use GPS satellites) options of android device programatically. – Prem Singh Bist Jan 24 '12 at 09:35