如何*正确*请求权限?

时间:2019-05-24 11:53:03

标签: java android android-permissions

我正在开发一个Android应用,该应用在启动时需要3个权限。我必须等到用户在对话框中做出响应,否则,由于权限是必需的,应用程序将崩溃。

请求这些权限的正确方法是什么?我编写了以下代码,但是在响应第一个权限(拒绝或接受)时,应用程序冻结。

这就是我做的:

    String[] perms = {"Manifest.permission.READ_PHONE_STATE", "Manifest.permission.ACCESS_COARSE_LOCATION","Manifest.permission.CALL_PHONE"};

    int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE);
    int result2 = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION);
    int result3 = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE);

    if ((result1 == PackageManager.PERMISSION_GRANTED) && (result2 == PackageManager.PERMISSION_GRANTED) && (result3 == PackageManager.PERMISSION_GRANTED)) {
        onPermissionGranted();
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(perms, PERMISSION_REQUEST_CODE);
        } else {
            ActivityCompat.requestPermissions(this, perms, PERMISSION_REQUEST_CODE);
        }

    }

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                onPermissionGranted();
                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {
                Toast.makeText(MainActivity.this, R.string.permission_required, Toast.LENGTH_LONG).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
    }
}

5 个答案:

答案 0 :(得分:3)

由于权限请求是异步的,因此无需使用Thread#sleep或while循环进行阻止。这是您的活动应如何进行的:

首先,定义需要权限才能运行的逻辑:

private void onPermissionGranted() {
   //code to execute
}

然后在onCreate()中,检查您是否具有这些权限。如果这样做,请执行代码。如果您不这样做,请请求权限:

if (ContextCompat.checkSelfPermission(this,/*permissions*/) == PackageManager.PERMISSION_GRANTED) {
    onPermissionGranted();
} else {
   ActivityCompat.requestPermissions(this, /*permissions*/, /*request_code*/); 
}

权限状态更新后,它会直拨到onRequestPermissionsResult。如果授予了权限,请在此处执行操作。否则,请执行其他操作(重新显示对话框或完成活动)。

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case /*our request code from before*/: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                onPermissionGranted();
            } else {
                Toast.makeText(this, "Permissions are required in order to run this app!", Toast.LENGTH_SHORT).show();
                finish();
            }
            return;
        }
    }
}

答案 1 :(得分:2)

要让android获得权限,您需要进入android清单,然后添加您的权限

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

答案 2 :(得分:1)

根据official docs的请求权限,首先检查是否授予了权限:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
}

,如果未获得请求,则:

ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);

最后使用以下代码处理请求的结果:

@Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay!
            } 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.
    }
}

答案 3 :(得分:1)

转到android清单并添加 这适用于xamarin和统一性

答案 4 :(得分:-1)

最简单的方法是使用某些库。例如,您可以使用TedPersmission。真的很容易使用。

如果您想自己实现逻辑而不使用任何库,那么您应该请求许可,并在许可被授予/拒绝时有一个监听器(onRequestPermissionsResult),如果被拒绝,则通知用户,不要让他继续。如果被授予,则请求第二个权限,并让第二个侦听器查看用户是否授予了此权限,依此类推,直到用户授予/拒绝了您需要的所有权限。