调用需要API级别23(当前最小为14):android.app.Activity#requestPermissions,checkSelfPermission

我试图添加运行时权限android(6.0.1)API 23,如果我使用SDK版本( 最小和目标版本都23 )它很好,像下面,

<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" /> 

如果我更改android:minSdkVersion(小于23)

例如:

我在下面的错误:

调用需要API级别23(当前最小为14):android.app.Activity#requestPermissions,checkSelfPermission

以下两种方法,

 1)requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) 2)checkSelfPermission(permission) 

我试过ActivityCompat.checkSelfPermission()ContextCompat.checkSelfPermission()都不起作用。

我想念的不明白..

检查目标> = 23,或者简单地在你的方法上方添加下面的行

 @TargetApi(Build.VERSION_CODES.M) 

例如,如果你正在检查存储权限,那么你可以参考这个函数,

 @TargetApi(Build.VERSION_CODES.M) public boolean CheckStoragePermission() { int permissionCheckRead = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE); if (permissionCheckRead != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) { // 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. ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Define.PERMISSION_STORAGE); } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Define.PERMISSION_STORAGE); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } return false; } else return true; } 

检查权限:

ContextCompat.checkSelfPermission(Context context, String permission)

请求权限:

ActivityCompat.requestPermissions(Activity activity, String[] permissions, int requestCode)

或在一个支持v4 Fragment

requestPermissions(String[] permissions, int requestCode)

试试这个方法

 if (Build.VERSION.SDK_INT < 23) { // your code } else { if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { requestContactsPermissions1(); } else { // your code } } 

编辑:

在较低级别(您的应用程序) build.gradle添加相关性块:

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

或仅在需要devise支持库的情况下添加以下内容

 compile 'com.android.support:design:23.1.1' 

使用上面的一个

这是因为checkSelfPermission()被添加到API 23中 。 所以你必须有条件的编码,如:

 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){ //use checkSelfPermission() } else { //simply use the required feature //as the user has already granted permission to them during installation } 

在调用请求权限之前检查设备sdk

  if(Build.VERSION.SDK_INT==Build.VERSION_CODES.M) { //call the request permission here }