Turn on location providers programmatically in Android

Can I enable DeviceProviders (a GPS / network service provider) on a device programmatically if it is disabled?

+7
source share
6 answers

No, it’s not, but you can open the location services settings window:

context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
+20
source

Turn on a location with High Accuracy or save the battery without having to visit the settings

http://android-developers.blogspot.in/2015/03/google-play-services-70-places-everyone.html

+6
source
 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 only supports up to APK 8.

+2
source

Yes, but you must be superuser (sudo), your device must be rooted.

+1
source

With the recent Marshmallow update, even if location adjustment is enabled, your application will need to explicitly request permission. The recommended way to do this is to show the "Permissions" section of your application, in which the user can switch the resolution as necessary. The code snippet for this is as follows:

 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 shown 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 - you can also use SettingsApi to find out which location providers are included. If none are activated, you can request a dialog to change settings from the application.

0
source

Use the following code to launch 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 ); 

See the documentation for the LocationManager and for the LocationListener for more information.

-one
source

All Articles