0

I am following this question's answer to solve my problem but the issue is onActivity result it always go to else statement and no dialog box appear where user can allow permission.

How to request Location Permission at runtime on Android 6

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;


    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                new AlertDialog.Builder(this)
                        .setTitle(R.string.title_location_permission)
                        .setMessage(R.string.text_location_permission)
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //Prompt the user once explanation has been shown
                                ActivityCompat.requestPermissions(SalesPersonActivity.this,
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        MY_PERMISSIONS_REQUEST_LOCATION);
                            }
                        })
                        .create()
                        .show();


            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // location-related task you need to do.
                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            == PackageManager.PERMISSION_GRANTED) {

                        //Request location updates:
                            getLocation();
                    }

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    checkLocationPermission();
                }
                return;
            }

        }
halfer
  • 19,824
  • 17
  • 99
  • 186
Waqas Arif
  • 59
  • 1
  • 7

1 Answers1

0

Just answered before 5 minutes link

Use common code for asking permission, so that you use it anywhere in your project when you need it.

You have 2 ways

  1. Follow correct way to ask permission. I am posting my code snippet, copy this code in your Base Activity class.
  2. Or you use RxPermissions. If you are new to Android.

Here is my code snippet.

String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};

        getBulkPermissions(permissions, new RequestPermissionAction() {
            @Override
            public void permissionDenied() {
                // TODO: 5/27/2018 handle permission deny 
            }

            @Override
            public void permissionGranted() {
                // TODO: 5/27/2018 you code do further operations 
            }
        });

Can it be more simpler?

Now you will put this code Base Activity class and use anywhere you need. Or put in your current class if you don't need it at any other place.

RequestPermissionAction onPermissionCallBack;
private final static int REQUEST_BULK_PERMISSION = 3006;

private boolean checkBulkPermissions(String[] permissions) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        for (String permission : permissions) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    } else {
        return true;
    }
}

public void getBulkPermissions(String[] permissions, RequestPermissionAction onPermissionCallBack) {
    this.onPermissionCallBack = onPermissionCallBack;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!checkBulkPermissions(permissions)) {
            requestPermissions(permissions, REQUEST_BULK_PERMISSION);
            return;
        }
    }
    if (onPermissionCallBack != null)
        onPermissionCallBack.permissionGranted();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        if (onPermissionCallBack != null)
            onPermissionCallBack.permissionGranted();
    } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
        if (onPermissionCallBack != null)
            onPermissionCallBack.permissionDenied();
    }
}

public interface RequestPermissionAction {
    void permissionDenied();
    void permissionGranted();
}

Also check whether you need ACCESS_COURSE_LOCATION permission too.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212