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

15

我创建了一个简单的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){
            }
        }
    });
}

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

  • 如果我点击呼叫按钮并授权,意图直到我再次点击才被调用

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


1
您可以尝试访问以下链接:http://stackoverflow.com/questions/42136340/not-able-to-get-android-run-time-permission-result/42136548#42136548,这是与Android运行时权限结果相关的编程内容。 - vimal raj
2个回答

17
使用onRequestPermissionResult,它可以处理用户按下允许拒绝的动作,只需在“如果用户按下允许”的条件中调用意图即可。
@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;
        }
    }
}

希望这可以帮到你。


我尝试了一下,但是当我拒绝权限时,Toast 没有显示。 - Amine Messaoudi
现在请检查。 - tahsinRupam
如果有多个权限,您需要从“permissions”参数中找到索引,并将其用于“grantResults”。 - atjua

16

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)

or

<uses-permission android:name="android.permission.CAMERA"/>

in the AndroidManifest.xml file to properly check for permission status.

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接