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

shstlldc  于 2023-05-12  发布在  Android
关注(0)|答案(2)|浏览(242)

我创建了一个简单的Android活动,充当拨号。它有一个编辑文本的电话号码和一个呼叫按钮这里是代码:(安卓6.0棉花糖)

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){
            }
        }
    });
}

}
当我运行我的应用程序时,我遇到了以下问题

  • 如果我单击调用按钮并授予权限,则在我再次单击之前不会调用Intent
  • 我不知道如何检查是否已授予权限
hyrbngr7

hyrbngr71#

使用onRequestPermissionResult,它处理用户按下ALLOWDENY的动作,只需调用条件“如果用户按下allow”中的Intent:

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

希望这能帮上忙。

sg3maiej

sg3maiej2#

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方法中。

相关问题