1

I am trying to access the users locations and just sorting out the permission side of things. So my understanding is that on 23 and above regardless of the manifest the user must grant permission.

So I have it in the manifest like this for older versions:

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

For the newer >= 23 I am testing like this before showing a dialog:

// Check for runtime location permissions
private boolean hasRunTimeLocationPermission() {
    int courseLocationPermission = ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION);
    return  (courseLocationPermission == PackageManager.PERMISSION_GRANTED );
}

The way I understood it that should return false the first time but it is returning true.

Does the user actually have to disable location services or is it considered 'dangerous" and it have to be approved the first time?

Also I am using a new emulator api 23, when I look into location permissions it says no apps have requested location.

Thanks for your help

Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89

4 Answers4

0

Try this.

// Check for runtime location permissions
private boolean hasRunTimeLocationPermission() {
    int courseLocationPermission = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION);
    return  (courseLocationPermission == PackageManager.PERMISSION_GRANTED );
}
vidulaJ
  • 1,232
  • 1
  • 16
  • 31
0

TRY THIS:

private boolean hasRunTimeLocationPermission() {
 if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED)
        {
            return false;
        }
 else
        {
           return true;
        }
      }
0

Check permissions like this :-

Check permissions like this :-

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            //Show Information about why you need the permission
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Need Location Permission");
            builder.setMessage("This app needs location permission.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_COARSE_LOCATION_CONSTANT);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else if (permissionStatus.getBoolean(Manifest.permission.ACCESS_COARSE_LOCATION,false)) {
            //Previously Permission Request was cancelled with 'Dont Ask Again',
            // Redirect to Settings after showing Information about why you need the permission
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Need Location Permission");
            builder.setMessage("This app needs location permission.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    sentToSettings = true;
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                    Toast.makeText(getBaseContext(), "Go to Permissions to Grant Location", Toast.LENGTH_LONG).show();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        } else {
            //just request the permission
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, ACCESS_COARSE_LOCATION_CONSTANT);
        }

        SharedPreferences.Editor editor = permissionStatus.edit();
        editor.putBoolean(Manifest.permission.ACCESS_COARSE_LOCATION,true);
        editor.commit();


    } else {
        //You already have the permission, just go ahead.
        proceedAfterPermission();
    }
0

Add this to Manifest even for api>23

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

And then ask for runtime permissions like this :-

ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                1);

And to handle the restults, use this:-

@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults) {
switch (requestCode) {
    case 1: {

      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
        } else {

            // permission denied, boo! 
            Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
        }
        return;
    }
}
}