我创建了一个简单的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){
}
}
});
}
当我运行我的应用程序时,我遇到了以下问题:
如果我点击呼叫按钮并授权,意图直到我再次点击才被调用
我不知道如何检查权限是否已被授予