如何检查用户是否在Android上的运行时授予了权限? [英] How to check if permission is granted by user at runtime on Android?

查看:98
本文介绍了如何检查用户是否在Android上的运行时授予了权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的android活动,用作拨号.它具有电话号码的编辑文本和呼叫按钮这是代码:(Android 6.0棉花糖)

I have created a simple android activity that acts as a dial. It has an edit text for the phone number and a call button Here is the code : (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){
            }
        }
    });
}

}

运行我的应用程序时,我遇到了这些问题

When I run my app, I have these issues

  • 如果我单击呼叫"按钮并授予权限,则直到再次单击该意图才被调用

  • If I click on the call button and grant permission, the intent is not called until I click again

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

I don't know how to check if the permission was granted or not

推荐答案

使用onRequestPermissionResult,如果用户按下 ALLOW DENY ,它将处理操作,只需在条件如果用户按下允许":

Use onRequestPermissionResult, It handles the action if user press ALLOW and DENY, Just call the intent in the condition "if the user presses 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;
            }
        }
    }

希望这会有所帮助.

这篇关于如何检查用户是否在Android上的运行时授予了权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆