如何检查用户是否在运行时在Android上授予了权限?

时间:2017-03-13 11:17:13

标签: android permissions runtime grant

我创建了一个简单的android活动,充当拨号盘。 它有一个用于电话号码的编辑文本和一个呼叫按钮 这是代码:(android 6.0 marshmallow)

public class Main2Activity extends AppCompatActivity {
EditText num;
Button call;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    num = (EditText) findViewById(R.id.num);
    call = (Button) findViewById(R.id.call);
    call.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                // request permission if not granted
                if (ActivityCompat.checkSelfPermission(Main2Activity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(Main2Activity.this, new String[]{Manifest.permission.CALL_PHONE}, 123);
                    // i suppose that the user has granted the permission
                    Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
                    startActivity(in);
                 // if the permission is granted then ok
                } else {
                    Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
                    startActivity(in);
                }
            }
            // catch the exception if i try to make a call and the permission is not granted
            catch (Exception e){
            }
        }
    });
}

}

当我运行我的应用时,必须发布

  • 如果我点击通话按钮并授予权限,则在我再次点击之前不会调用该意图

  • 我不知道如何检查是否授予了权限

2 个答案:

答案 0 :(得分:4)

使用onRequestPermissionResult,如果用户按允许 DENY ,它会处理操作,只需在“如果用户按下允许”条件下调用意图:

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 123: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   //If user presses allow
                   Toast.makeText(Main2Activity.this, "Permission granted!", Toast.LENGTH_SHORT).show();
                   Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
                startActivity(in);
                } else {
                   //If user presses deny
                   Toast.makeText(Main2Activity.this, "Permission denied", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
    }

希望这有帮助。

答案 1 :(得分:0)

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)是检查许可权结果的糟糕逻辑,我不知道Google为什么实施如此糟糕的代码。

特别是当您检查多个权限时,情况一片混乱。假设您要 CAMERAACCESS_FINE_LOCATIONACCESS_NETWORK_STATE

您需要检查ACCESS_FINE_LOCATION,但用户仅在第一次运行时才授予CAMERA,并且您要检查GrantResults [1],但在第二次运行中,ACCESS_FINE_LOCATION成为索引为0的权限。

您应该使用

   int size = permissions.length;
    boolean locationPermissionGranted = false;

    for (int i = 0; i < size; i++) {
        if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION)
                && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
            locationPermissionGranted = true;
        }
    }

或更简单的一个

   if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
           // Do something ...
        }

采用onPermissionRequestResult方法。