6
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Context context = (Permission) this;
    // In an actual app, you'd want to request a permission when the user
    // performs an action
    // that requires that permission.
    if (Build.VERSION.SDK_INT >= 23) {
        getPermissionToReadUserContacts();
    }
}

// Identifier for the permission request
private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;

// Called when the user is performing an action which requires the app to
// read the
// user's contacts
public void getPermissionToReadUserContacts() {
    // 1) Use the support library version
    // ContextCompat.checkSelfPermission(...) to avoid
    // checking the build version since Context.checkSelfPermission(...) is
    // only available
    // in Marshmallow
    // 2) Always check for permission (even if permission has already been
    // granted)
    // since the user can revoke permissions at any time through Settings
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

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

            // Show an expanation 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.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[] { Manifest.permission.READ_CONTACTS },
                    READ_CONTACTS_PERMISSIONS_REQUEST);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
    case READ_CONTACTS_PERMISSIONS_REQUEST: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Read Contacts permission granted",
                    Toast.LENGTH_SHORT).show();
            // permission was granted, yay! Do the
            // contacts-related task you need to do.

        } else {
            Toast.makeText(this, "Read Contacts permission denied",
                    Toast.LENGTH_SHORT).show();
            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
    }
}
// Callback with the request from calling requestPermissions(...)
/*
 * @Override public void onRequestPermissionsResult(int requestCode, String
 * permissions[], int[] grantResults) { // Make sure it's our original
 * READ_CONTACTS request if (requestCode ==
 * READ_CONTACTS_PERMISSIONS_REQUEST) { if (grantResults.length == 1 &&
 * grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 * Toast.makeText(this, "Read Contacts permission granted",
 * Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this,
 * "Read Contacts permission denied", Toast.LENGTH_SHORT).show(); } } else {
 * super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 * } }
 */

When i try this in a demo app it is working fine.But when i include in the project it is throwing a error as title.Any help will be appreciated.It is because of context i think so. ..........................................................................................................................................................................................................................................................................................................................................................

Naresh Reddy
  • 65
  • 1
  • 1
  • 5

4 Answers4

4

ContextCompat.checkSelfPermission() requires support library in version 23 or higher.

Add in dependencies block in lower-level build.gradle:

compile 'com.android.support:appcompat-v7:23.1.1' 

If you have previous version here (which you probably have), you should replace lane with provided by me.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
  • 4
    How do I solve this with Eclipse where there are no gradle build files? I have the V7 version of appcompat downloaded by the Android SDK Manager? I added the android-support-v7-appcompat.jar to my project. The eclipse IDE error that I get is "The method checkSelfPermission(MyActivity, String) is undefined by the type ContextCompat In my imports: import android.support.v7.app.* import android.support.v7.appcompat.* import android.support.v4.content.ContextCompat – neuman8 Feb 18 '16 at 19:11
  • 5
    I solved my own issue. I was not importing the new version of android-support-v4.jar as well. Google has made many changes to that .jar since the last time I pulled it into my project. – neuman8 Feb 18 '16 at 21:16
  • 1
    can you provide me jar Link so,i can integrate runtime permition using eclipse? – Vasant Jun 20 '16 at 12:22
  • 1
    @Vasant You need to download library using Android SDK and then import it on your own - https://developer.android.com/topic/libraries/support-library/setup.html – Damian Kozlak Jun 20 '16 at 14:03
  • @DamianKozlak [The link](https://developer.android.com/topic/libraries/support-library/setup.html) to the Support Library Setup is priceless. However, downloading via Android SDK is no longer supported and one must resort converting the .aar from Google's Maven repository using https://stackoverflow.com/a/37747277/2597758 – WebViewer Dec 07 '22 at 20:32
2

This issue occurs because the support library is out of date.

If you're using Eclipse, I've found the most simple solution is to right-click the project and go to:

Android Tools > Add support library...

If this doesn't resolve the issue, you probably need to update the SDK first:

Window > Android SDK Manager > Install Updates

The support library will be added/updated and will no longer show this error.

Webkraft Studios
  • 492
  • 5
  • 18
  • 1
    1st solution did help me in Eclipse. After adding support libraries do not forget to restart Eclipse though. TY – Boris Gafurov Aug 29 '18 at 14:26
  • 1
    My 'Android SDK Manager' shows that the installed 'Android Support Library' only goes up to `22.1.1`. The accepted answer states that this feature requires support library in version 23 or higher. How do solve this? Did you actually mean `Help > Install New Software > Work with: Android Developer Tools Update Site` ? – WebViewer Dec 07 '22 at 18:40
0

You can set target android of your project to android 6.0 or higher. Then add android-support-v4.jar

Tru Nguyen
  • 11
  • 2
0

In the accepted answer by @DamianKozlak, "support library" version 23 or higher is required.

My 'Android SDK Manager' shows that the installed 'Android Support Library' only goes up to 22.1.1

The answer by @ebkraftStudios suggests to update it via Window > Android SDK Manager > Install Updates but... There is no such path in my Eclipse+ADT setup.

Instead, I found that Help > Install New Software > Work with: Android Developer Tools Update Site suggests the desired update (23.0.7.2120684):

enter image description here

WebViewer
  • 761
  • 7
  • 21