运行时权限不起作用

时间:2017-09-04 09:48:20

标签: android runtime-permissions

这是我在OnCreate()方法中的代码:

if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.RECIEVE_SMS)) {

} else {

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

在我的onRequestPermissionsResult()方法中:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

              init();
}else{
         finish();
 }

我的清单文件具有以下权限

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>

但是我没有得到要求许可的对话框,我无法弄清楚问题?

2 个答案:

答案 0 :(得分:2)

如果您的应用针对Android 6.0及以上版本而不是添加运行时权限

从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限,而不是在他们安装应用时授予

使用以下代码为READ_SMS添加运行时权限

String permission = Manifest.permission.READ_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
    String[] permission_list = new String[1];
    permission_list[0] = permission;
    ActivityCompat.requestPermissions(this, permission_list, 1);
}

并处理这样的结果

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();  
             // perform your action here

        } else {
            Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
        }
    }

}

答案 1 :(得分:0)

试试这个:

if (ActivityCompat.checkSelfPermission(AddnewPhotoActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(AddnewPhotoActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);

            } else {

                isPermissionGranted(true);
            }

<强> isPermissionGranted(布尔值); ====&GT;方法调用

public void isPermissionGranted(boolean permission) {
        if (permission) {


           //Your Operation


        } else {

            Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
        }
    }

<强> onRequestPermissionsResult

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_READ_EXTERNAL_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                isPermissionGranted(true);

            } else

            {
                isPermissionGranted(false);
            }
        }

    }