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

纳雷什·雷迪(Naresh Reddy)
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);
 * } }
 */

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

达米安·科兹拉克(Damian Kozlak)

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

dependencies在较低级别添加build.gradle

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

如果这里有以前的版本(可能已经有),则应使用我提供的版本替换lane。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

对于IOUtils类型,未定义方法toByteArray(InputStream)

对于类型JdbcTemplate,未定义方法queryForInt(String)

对于类型HttpServletRequest的getDispatcherType()未定义

对于类型为Button的方法,未定义方法getText()

对于类型AlertDialog.Builder,未定义方法getWindow()

对于类型ClassName,未定义方法getId()

对于T类型,该方法未定义

对于类型为zip4j的ZipFile,未定义方法extractAll(String)

对于类型SMSReceiver,未定义方法abortBroadcast()

对于类型为String的错误,未定义方法getText()

对于类型AlertDemo,未定义方法getSystemService(String)

对于新的Runnable(){}类型,未定义方法getActivity()

对于ParcelFileDescriptor类型,未定义fromDatagramSocket(DatagramSocket)方法

对于类型NetworkChangeReceiver,未定义方法isNetworkAvailable(Context)

对于JPanel类型,未定义方法setText(String)

对于SharedPreferences类型,该方法未定义

对于类型EditText,未定义方法getTexttActivity(Intent)

对于类型Robolectric,未定义方法buildActivity(Class <MainActivity>)

对于Intent类型,未定义方法putStringExtra(String,String)

对于类型MainActivity,未定义方法getSupportFragmentManager():android

对于类型Set,未定义方法get(int)

对于类型HttpServletRequest,未定义方法getPart(String)

对于DrawerLayout类型,未定义方法setStatusBarBackgroundColor()

对于类型MainActivity,未定义方法getParcelableArrayList(String)

对于JSONObject类型,未定义getJSONObject(String)

对于类型 Scanner,方法 nextInt() 未定义

方法 compareTo(ITestNGMethod) 对于 ITestNGMethod 类型是未定义的

方法 getFontMetrics(Font) 对于 infoPanel 类型是未定义的

方法 getType() 对于类型 Ingredient 未定义