Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

onrequestpermissionsresult method grantResults[0] always return -1 in both case allow and deny please check my code below:-

Requesting for location permission:-

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                int hasReadStatePermission = getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
                if (hasReadStatePermission != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, Constants.RequestCode.KEY_PERMISSION_ACCESS_FINE_LOCATION);
                }else {
                    setLocation();
            }else {
                DialogUtils.dialogTwoButton(getActivity(), "Allow", "Deny", getString(R.string.msg_get_gps_access), new Runnable() {
                    @Override
                    public void run() {
                        setLocation();
                }, new Runnable() {
                    @Override
                    public void run() {
                        Intent intent=new Intent(getActivity(), ChangeLocationManualyActivity.class);
                        startActivityForResult(intent,Constants.RequestCode.LOCATION_CHANGE);
  

Permission callback method where PackageManager.PERMISSION_GRANTED=0, PackageManager.PERMISSION_DENIED=-1

  @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode==Constants.RequestCode.KEY_PERMISSION_ACCESS_FINE_LOCATION)
            if(grantResults.length>0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setLocation();
            }else{
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent=new Intent(getActivity(), ChangeLocationManualyActivity.class);
                        startActivityForResult(intent,Constants.RequestCode.LOCATION_CHANGE);
                },3000);
                sorry for late comment the problem is Occurred because i am set manifest permission only for      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> and on runtime request for both Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION.
– Dalvendra Singh
                May 18, 2017 at 5:40

Please check with following code:-

  public static boolean selfPermissionGranted(Context context, String permission) {
        // For Android < Android M, self permissions are always granted.
        boolean result = true;
        int targetSdkVersion;
        try {
            final PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            targetSdkVersion = info.applicationInfo.targetSdkVersion;
        } catch (PackageManager.NameNotFoundException e) {
            targetSdkVersion = 21;
            e.printStackTrace();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (targetSdkVersion >= Build.VERSION_CODES.M) {
                // targetSdkVersion >= Android M, we can
                // use Context#checkSelfPermission
                result = context.checkSelfPermission(permission)
                        == PackageManager.PERMISSION_GRANTED;
            } else {
                // targetSdkVersion < Android M, we have to use PermissionChecker
                result = PermissionChecker.checkSelfPermission(context, permission)
                        == PermissionChecker.PERMISSION_GRANTED;
        System.out.println("Result is==>" + result);
        return result;

Give call like below:-

if (!selfPermissionGranted(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        PERMISSION_ACCESS_COARSE_LOCATION);

Its worked for me.Hope it helps you.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.