对于ContextCompat类型,方法checkSelfPermission(Context,String)是未定义的

Nar*_*ddy 5 android android-activity android-permissions android-6.0-marshmallow

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);
 * } }
 */
Run Code Online (Sandbox Code Playgroud)

当我在一个演示应用程序中尝试这个时它工作正常.但是当我在项目中包含它时会抛出一个错误作为标题.任何帮助都将受到赞赏.这是因为上下文我认为如此................................................... .................................................. .................................................. .................................................. .................................................. .................................................. ..............................................

Dam*_*lak 4

ContextCompat.checkSelfPermission()需要版本 23 或更高版本的支持库。

dependencies在下层块中添加build.gradle

compile 'com.android.support:appcompat-v7:23.1.1' 
Run Code Online (Sandbox Code Playgroud)

如果您在这里有以前的版本(您可能有),您应该将 Lane 替换为我提供的。

  • 我解决了我自己的问题。我也没有导入新版本的 android-support-v4.jar 。自从我上次将 .jar 引入我的项目以来,Google 对该 .jar 进行了许多更改。 (5认同)
  • 在没有 gradle 构建文件的情况下,如何使用 Eclipse 解决这个问题?我有Android SDK Manager下载的V7版本的appcompat吗?我将 android-support-v7-appcompat.jar 添加到我的项目中。我得到的 Eclipse IDE 错误是“方法 checkSelfPermission(MyActivity, String) 未由类型 ContextCompat 在我的导入中定义: import android.support.v7.app.* import android.support.v7.appcompat.* import android. support.v4.content.ContextCompat (4认同)