如何让应用检查是否授予了权限(6,x +)Android

时间:2016-11-15 21:47:37

标签: java android android-studio android-manifest android-permissions

我有一个短信应用程序,现在6.0,用户必须允许该应用程序发送短信。 现在我有这个代码,我从link here获得。

 int MY_PERMISSIONS_REQUEST_READ_CONTACTS=0;
// Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.SEND_SMS)) {

                // 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.SEND_SMS},
                        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.
            }
        }

但是当我拒绝接听时(如果用户意外地按下了按钮),每次操作都会使应用程序崩溃(导致短信按钮不起作用)。因此,当用户在拒绝访问后启动/重启应用程序时,我希望应用程序再次询问。现在,当我尝试在我的模拟器上没有对话框会再次出现,所以我不得不去设置/应用程序并设置权限。或者再次卸载并安装。

2 个答案:

答案 0 :(得分:0)

您当前正在做的是询问用户是否允许,但不检查是否实际授予了权限。您可以通过在请求权限的活动中实施const hashObj = location.hash.replace('#', '').split('&').reduce((prev, item) => { return Object.assign({[item.split('=')[0]]: item.split('=')[1]}, prev); }, {}); 方法来执行此操作。

有关详细信息,请参阅https://stackoverflow.com/a/34722591/3872500

答案 1 :(得分:0)

使用来自工作应用的不同权限的示例,其中包含您需要进行更改的注释:

if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    progressLayout.setVisibility(View.GONE);
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)) {

        //This is where you need to ask again if they want to allow the permission,
        //you should show a dialog here to ask if they're sure
        //and explain why you need the permission.
        requestDialog = new AlertDialog.Builder(this)
                .setTitle(getString(R.string.device_setup_location_permission_title))
                .setMessage(getString(R.string.device_setup_location_permission_message))
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //They clicked yes, they want to enable the permission,
                        //so let's show them the dialog again.
                        ActivityCompat.requestPermissions(DeviceSetupActivity.this,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                REQUEST_LOCATION_RESPONSE);
                    }
                })
                .setNegativeButton(getString(R.string.later), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //This means they don't want to grant the permission, 
                        //you should exit the app here, perhaps share another dialog saying that in
                        //order to use the app, the permission must be granted.
                        finish();
                    }
                })
                .show();

    } else {

        // No explanation needed, we can request the permission.

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

        // 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, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == REQUEST_LOCATION_RESPONSE){
        if(hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)){
            startScan();
        }else{
            //exit the activity
            finish();
        }
    }
}

private boolean hasPermission(String perm) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
    }
    return true;
}