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

Joe*_*Joe 26 android android-permissions android-support-library android-6.0-marshmallow

我正在尝试添加运行时权限android(6.0.1)API 23,如果我使用SDK版本(min和目标版本都是23)它就好了,如下所示,

    <uses-sdk
                android:minSdkVersion="23"
                android:targetSdkVersion="23" />
Run Code Online (Sandbox Code Playgroud)

如果我改变android:minSdkVersion(少于23)

例如:

我收到以下错误:

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

对于以下2种方法,

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

2)checkSelfPermission(permission)
Run Code Online (Sandbox Code Playgroud)

我试过了ActivityCompat.checkSelfPermission(),ContextCompat.checkSelfPermission()两个都没有用.

我遗失的东西无法理解..

Ego*_*gor 34

检查权限:

ContextCompat.checkSelfPermission(Context context, String permission)

请求权限:

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

或者在支持-4内 Fragment

requestPermissions(String[] permissions, int requestCode)


小智 33

检查目标> = 23或只是在方法上方添加以下行

@TargetApi(Build.VERSION_CODES.M)
Run Code Online (Sandbox Code Playgroud)

例如,如果要检查存储权限,则可以参考此功能,

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


Dha*_*mar 14

试试这种方式

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

编辑:

在较低级别(您的应用程序)中添加依赖项块build.gradle:

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

或者仅在您需要设计支持库时添加以下内容

compile 'com.android.support:design:23.1.1'
Run Code Online (Sandbox Code Playgroud)

使用上述之一


cam*_*der 5

那是因为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
}
Run Code Online (Sandbox Code Playgroud)