Cannot resolve method checkSelfPermission

后端 未结 7 1095
南方客
南方客 2021-02-19 14:58

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:

相关标签:
7条回答
  • 2021-02-19 15:37

    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

    0 讨论(0)
  • 2021-02-19 15:46

    For Fragment use getActivity().checkSelfPermission

    For Activity use this..checkSelfPermission or simply checkSelfPermission

    0 讨论(0)
  • 2021-02-19 15:48

    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!

    0 讨论(0)
  • 2021-02-19 15:53

    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}

    0 讨论(0)
  • 2021-02-19 15:58
    @SuppressLint("NewApi")
    

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

    0 讨论(0)
  • 2021-02-19 15:59

    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'}*
    
    0 讨论(0)
提交回复
热议问题