Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android没有从电话簿读取的权限_Android_Android Studio_Permissions - Fatal编程技术网

Android没有从电话簿读取的权限

Android没有从电话簿读取的权限,android,android-studio,permissions,Android,Android Studio,Permissions,我是安卓新手。我想做一个功能,检查我的电话簿中是否已经有电话号码。 我在清单上添加了“读取联系人”权限: <uses-permission android:name="android.permission.READ_CONTACTS" /> 我无法获得读取联系人的权限,我的应用程序每次都会崩溃。基本上,在执行危险权限列表中的操作之前,您必须检查自己是否拥有权限 int permissionCheck = ContextCompat.checkSelfPermission(thi

我是安卓新手。我想做一个功能,检查我的电话簿中是否已经有电话号码。 我在清单上添加了“读取联系人”权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />


我无法获得读取联系人的权限,我的应用程序每次都会崩溃。

基本上,在执行危险权限列表中的操作之前,您必须检查自己是否拥有权限

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.WRITE_CALENDAR);
如果它返回你的应用程序未被授权,那么你需要在运行时请求它

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);
因此,将上面的代码放在“else”块中第一个“if”语句之后


如果您还有任何问题,请告诉我。

您可以在Android Marshmallow中了解有关新权限模型的所有信息

总结 在你尝试做任何事情之前,你必须检查你是否有权限

使用以下代码检查并请求权限:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            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(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // 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 MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

permissionCheck改为hasPermission???我的权限、请求、阅读、联系人有一个error@AlexTech最好使用ContextCompat.checkSelfPermission()检查您的权限,MY_PERMISSION\u REQUEST\u READ\u CONTACTS可以是任意整数。如果需要,您将需要它来处理请求权限。我看到了这个解决方案,但我需要获得许可,并在我的方法内完成工作。您的解决方案使我能够在onRequestPermissionsResult中执行操作