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

Ami*_*udi 4 permissions android 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){
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

}

当我运行我的应用程序时,必须发出问题

  • 如果我单击“呼叫”按钮并授予许可,则直到再次单击该意图才被调用

  • 我不知道如何检查是否已授予许可

Thr*_*ian 8

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

这是一团糟,尤其是当您检查多个权限时。假设您要求 CAMERA,ACCESS_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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

或者更简单的

   if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
           // Do something ...
        }
Run Code Online (Sandbox Code Playgroud)

onPermissionRequestResult方法。


tah*_*pam 7

使用onRequestPermissionResult,如果用户按下ALLOWDENY,它将处理操作,只需在“如果用户按下allow”条件下调用该意图:

@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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。