Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Location access programmatically in android?

I am working on map related android application and I need to check location access enable or not in client side development if location services is not enable show the dialog prompt.

How to enable "Location access" Programmatically in android?

like image 372
NarasimhaKolla Avatar asked Aug 07 '14 06:08

NarasimhaKolla


People also ask

How do I enable location permissions 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 I enable Location services on Android?

Android UsersAccess your Android Settings menu. Select Location Services. Turn on "Allow Access to my Location."


2 Answers

Use below code to check. If it is disabled, dialog box will be generated

public void statusCheck() {     final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);      if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {         buildAlertMessageNoGps();      } }  private void buildAlertMessageNoGps() {     final AlertDialog.Builder builder = new AlertDialog.Builder(this);     builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")             .setCancelable(false)             .setPositiveButton("Yes", new DialogInterface.OnClickListener() {                 public void onClick(final DialogInterface dialog, final int id) {                     startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));                 }             })             .setNegativeButton("No", new DialogInterface.OnClickListener() {                 public void onClick(final DialogInterface dialog, final int id) {                     dialog.cancel();                 }             });     final AlertDialog alert = builder.create();     alert.show(); } 
like image 151
Gautam Avatar answered Sep 22 '22 05:09

Gautam


Here is a simple way of programmatically enabling location like Maps app:

protected void enableLocationSettings() {        LocationRequest locationRequest = LocationRequest.create()              .setInterval(LOCATION_UPDATE_INTERVAL)              .setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL)              .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);          LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()                 .addLocationRequest(locationRequest);          LocationServices                 .getSettingsClient(this)                 .checkLocationSettings(builder.build())                 .addOnSuccessListener(this, (LocationSettingsResponse response) -> {                     // startUpdatingLocation(...);                 })                 .addOnFailureListener(this, ex -> {                     if (ex instanceof ResolvableApiException) {                         // Location settings are NOT satisfied,  but this can be fixed  by showing the user a dialog.                         try {                             // Show the dialog by calling startResolutionForResult(),  and check the result in onActivityResult().                             ResolvableApiException resolvable = (ResolvableApiException) ex;                             resolvable.startResolutionForResult(TrackingListActivity.this, REQUEST_CODE_CHECK_SETTINGS);                         } catch (IntentSender.SendIntentException sendEx) {                             // Ignore the error.                         }                     }                 });  } 

And onActivityResult:

@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {     if (REQUEST_CODE_CHECK_SETTINGS == requestCode) {         if(Activity.RESULT_OK == resultCode){             //user clicked OK, you can startUpdatingLocation(...);          }else{             //user clicked cancel: informUserImportanceOfLocationAndPresentRequestAgain();         }     } } 

You can see the documentation here: https://developer.android.com/training/location/change-location-settings

like image 37
makata Avatar answered Sep 18 '22 05:09

makata