Phil
Phil

Reputation: 993

Cannot resolve method checkSelfPermission

I'm trying to make my app ready for Android 6 and now I'm stuck to the point where you need to request and check permissions.

I tried the following from the docs:

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);

The problem is that Android Studio says Cannot resolve method 'checkSelfPermission'.

I already included the appcompat and support lib. ContextCompat is known to AS but the method itself isn't known. I don't know what am I doing wrong - in another project I can write this method and it gets recognized.

TargetAPI is 23.

Does anyone know a solution?

Upvotes: 15

Views: 38513

Answers (7)

codingjeremy
codingjeremy

Reputation: 5741

Trying to use checkSelfPermission() in a Fragment with Kotlin and wondering how to get around Context being null?

Have a look at the sample below, and remember, before the Fragment is attached to the Activity, the Context will be null.

private fun fineLocationPermissionApproved(): Boolean {

    val context = context ?: return false

    return PackageManager.PERMISSION_GRANTED == checkSelfPermission(
        context,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
}

Upvotes: 0

P1x
P1x

Reputation: 1488

I had the same problem. In my case I added a library that was using an old appcompat version, then the compiler could not find the right appcompat.

To fix the problem I added the option {transitive = false} while importing the culprit library, and this fixed the problem.

Now I have:

api ('org.library.using.old.appcompat:1.0.1') {transitive = false}

Upvotes: 0

gavin
gavin

Reputation: 325

As silly as it maybe, it could be in the wrong place. I had the same problem. The bolded part is where I had originally put the code. The italic part is where it should have gone

locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        Log.i("-----------", location.toString());
    }
    **if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) != 
            PackageManager.PERMISSION_GRANTED) {'some code'}**
    }; 'End of LocationListener method
    *if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) != 
        PackageManager.PERMISSION_GRANTED) { 'some code'}*

Upvotes: 0

Fahad Jadun
Fahad Jadun

Reputation: 49

@SuppressLint("NewApi")

I simply used this on top of my page and it works for me...

Upvotes: 1

Ali Akram
Ali Akram

Reputation: 199

For Fragment use getActivity().checkSelfPermission

For Activity use this..checkSelfPermission or simply checkSelfPermission

Upvotes: 5

Phil
Phil

Reputation: 993

Oh my godness - what a stupid mistake.

AS imported the supportlib as a jar and this jar was from like 2014. I just replaced the jarimport with the real dependency and know it is working.

Thanks for your help guys!

Upvotes: 16

Manikanta
Manikanta

Reputation: 3421

Here is how you need to call in various scenarios,

In case of activity:

 ContextCompat.checkSelfPermission(MyActivity.this,
        Manifest.permission.WRITE_CALENDAR);

In case of fragment:

 ContextCompat.checkSelfPermission(getActivity(),
        Manifest.permission.WRITE_CALENDAR);

In case of any utility class use context:

 ContextCompat.checkSelfPermission(context,
        Manifest.permission.WRITE_CALENDAR);

Comment below for further information

Upvotes: 26

Related Questions