如何处理READ_EXTERNAL_STORAGE权限被拒绝

时间:2019-03-18 06:56:45

标签: permissions android-external-storage

My app is not responding if I click on deny read_external_storage dialog pop-up.

这是我的代码:

public Boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    return;
    }

}

//这是主要活动

public void onCreate (Bundle icicle){
    super.onCreate(icicle);

    isStoragePermissionGranted();


Boolean isAnswered = true;

        while(isAnswered){

            if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
                Log.d("isanswered", String.valueOf(isAnswered));
              //  finishAffinity();

            }
            else{
                isAnswered = false;
                Log.d("isanswered", String.valueOf(isAnswered));
            }
        }


        if( checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED ){
            finishAffinity();

}

如果我不使用while循环,则应用程序崩溃。如果权限被拒绝,我想退出应用程序。如何正确处理权限被拒绝? 有没有办法检查主要活动中的权限?

0 个答案:

没有答案