android 未定义ContextCompat类型的checkSelfPermission(Context,String)方法

eh57zj3b  于 2022-12-09  发布在  Android
关注(0)|答案(4)|浏览(359)
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);
 * } }
 */

当我在一个演示应用程序中尝试这个时,它工作正常。但是当我在项目中包含它时,它会抛出一个错误作为标题。任何帮助都将不胜感激。我认为这是因为上下文。........................................................................................................................................................................................................................................................................

20jt8wwn

20jt8wwn1#

ContextCompat.checkSelfPermission()需要版本23或更高版本的支持库。
dependencies块添加到低级别build.gradle中:

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

如果你有以前的版本在这里(你可能有),你应该取代lane与由我提供。

wr98u20j

wr98u20j2#

出现此问题的原因是支持库已过期。
如果您正在使用Eclipse,我发现最简单的解决方案是右键单击项目并转到:
Android Tools > Add support library...
如果这不能解决问题,您可能需要先更新SDK:
Window > Android SDK Manager > Install Updates
支持库将被添加/更新,并且不再显示此错误。

34gzjxbg

34gzjxbg3#

您可以将项目的目标android设置为android 6.0或更高版本,然后添加android-support-v4.jar

ddhy6vgd

ddhy6vgd4#

在@DamianKozlak接受的答案中,需要“支持库”版本23或更高版本。
我的“Android SDK管理器”显示安装的“Android支持库”最高只能升级到22.1.1
@ebkraftStudios的回答建议通过Window > Android SDK Manager > Install Updates更新它,但是...在我的Eclipse+ADT设置中没有这样的路径。
相反,我发现Help > Install New Software > Work with: Android Developer Tools Update Site建议了所需的更新(23.0.7.2120684):

相关问题