简体   繁体   中英

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?

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

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!

For Fragment use getActivity().checkSelfPermission

For Activity use this..checkSelfPermission or simply checkSelfPermission

@SuppressLint("NewApi")

我只是在我的页面顶部使用它,它对我有用......

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'}*

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}

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
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM