Skip to content Skip to sidebar Skip to footer

How To Ask For Location Permission In Android Studio?

I have tried so many codes and solutions but none of them worked. I want to ask the user for permission and get the pop-up window. Even though I used the code the window won't show

Solution 1:

From API level 23 (Android 6.0 Marshmallow) we need to ask Run-time permission from the user-end. You can do this in two steps.

1) Add these two permissions in AndroidManifest.xml file.

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

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

Example:

<manifestxlmns:android...>
 ...
 <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /><application...
</manifest>

2) Put this code block inside class where you need to get current location

if ( Build.VERSION.SDK_INT >= 23){
              if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
                          PackageManager.PERMISSION_GRANTED  ){
                   requestPermissions(newString[]{
                                      android.Manifest.permission.ACCESS_FINE_LOCATION},
                              REQUEST_CODE_ASK_PERMISSIONS);
                      return ;
                  }
              }

              getLocation();

  }
  //get access to location permissionfinalprivateintREQUEST_CODE_ASK_PERMISSIONS=123;



  @OverridepublicvoidonRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
      switch (requestCode) {
          case REQUEST_CODE_ASK_PERMISSIONS:
              if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  getLocation();
              } else {
                  // Permission Denied
                  Toast.makeText( this,"your message" , Toast.LENGTH_SHORT)
                          .show();
              }
              break;
          default:
              super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      }
  }

//Get locationpublicvoidgetLocation(){
      LocationManagerlocationManager= (LocationManager) getSystemService(LOCATION_SERVICE);
          Location myLocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
              if (myLocation == null) 
              {
                  myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

              }
  }

Solution 2:

if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted// Should we show an explanation?if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.ACCESS_FINE_LOCATION)) {

    } else {
        ActivityCompat.requestPermissions(thisActivity,
                newString[]{Manifest.permission.ACCESS_COARSE_LOCATION});
    }
} else {
    // Permission has already been granted
}

Post a Comment for "How To Ask For Location Permission In Android Studio?"