如何知道用户是否在提示时允许权限

时间:2017-01-23 07:06:07

标签: android

当弹出权限对话框时,它会询问我是否允许或拒绝某个权限。事情是,当我允许或否认时,动作不会执行。我如何知道他是否允许我接受后可以执行某项行动?

我试过了:

int hasWriteExternalStoragePermission = ctx.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (hasWriteExternalStoragePermission == PackageManager.PERMISSION_GRANTED) {
        // my code
    }

但它并没有执行,因为它太晚了#34;

3 个答案:

答案 0 :(得分:1)

您必须覆盖onRequestPermissionsResult功能,您才会收到permission Dialog pop up的回电。

如下所示

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
            } else {

                // permission denied
                // ask again or ignore

            }
            return;
        }

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

答案 1 :(得分:1)

您需要覆盖此方法

@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
    }
}

link到官方文档

答案 2 :(得分:0)

尝试这种方式:

public  boolean isStoragePermissionGranted() 
{
    if (Build.VERSION.SDK_INT >= 23) 
    {
        if (checkSelfPermission(android.Manifest.permission.WRITE_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.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else 
    { 
        //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted <23");
        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
    }

   else
   {
       Log.v(TAG,"Permission denied");
   }
}

这样,如果授予权限,它将来到这里:

  • Log.v(TAG,"Permission is granted");

如果尚未授予许可,则会出现:

  • Log.v(TAG,"Permission is revoked");并尝试获得许可。

如果用户在运行时授予权限:

  • Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);

如果用户拒绝它:

  • Log.v(TAG,"Permission denied");

如果版本低于Marshamallow,即低于API 23,那么它会来到这里:

  • Log.v(TAG,"Permission is granted <23");