Samet Dağ
Samet Dağ

Reputation: 167

checkSelfPermission always return 0 in Android

I have an app to get user location with permissions.My target API=25 btw

Once I have given permission that It doesn't ask me for request permission anymore.

I have checked with debug my hasCoarseLocationPermission and hasFineLocationPermission always return 0(GRANTED) after giving permission.

But if I clear the app data asking for permission again(returns -1).But I always want to ask for permission when I open the application(must return -1 not 0).How can i do that?

Thanks in advance.

int hasCoarseLocationPermission=ContextCompat.checkSelfPermission(Kirala3.this, Manifest.permission.ACCESS_COARSE_LOCATION);
int hasFineLocationPermission = ContextCompat.checkSelfPermission(Kirala3.this, Manifest.permission.ACCESS_FINE_LOCATION);

I've tried also with PermissionChecker function but it returns same value(0).

Upvotes: 0

Views: 2334

Answers (3)

Larry Schiefer
Larry Schiefer

Reputation: 15775

Asking the user for permissions is not something an app is supposed to do at each launch. Once the app has been granted a permission, it has the permission until it is revoked by the user. What you describe is happening sounds correct:

  1. First launch the app calls checkSelfPermission and gets back PERMISSION_DENIED
  2. The app requests the permission and is granted by the user
  3. Subsequent launches of the app also still call checkSelfPermission (which is exactly what it should do.) Now the returned value is PERMISSION_GRANTED. This is expected. No new permission request from the user is required.
  4. Manually reset the app data and revoke the permissions in the Settings app
  5. Launch the app again and checkSelfPermission returns PERMISSION_DENIED, the app must ask the user for permission again.

You may find this talk helpful: https://youtu.be/WGz-alwVh8A

You may also find this permissions helper library useful: https://github.com/hiqes/andele

Upvotes: 0

Vinayak B
Vinayak B

Reputation: 4510

Use this function for checking location permission

    public boolean isLocationPermissionEnabled() {

        return !(Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED);
    }

Upvotes: 0

Benjith Mathew
Benjith Mathew

Reputation: 1221

Try with ActivityCompat

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

Upvotes: 1

Related Questions