我创建了一个简单的android活动,充当拨号。 它有一个电话号码和一个呼叫按钮 这里一个EditText是代码:(安卓6.0棉花糖)如何检查权限是否由运行时用户在Android上授予?
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){
}
}
});
}
}
当我跑我的应用程序,有问题
如果我点击通话按钮并授予权限,则意图不会被调用,直到我再次点击
我不知道如何o或检查的权限被授予不
你可以尝试下面的链接http://stackoverflow.com/questions/42136340/not-able-to-get-android-run-time-permission-result/42136548#42136548 –