android 电话拨打,如何在Android中直接拨打电话
长时间过去了。但可能会帮助别人。 如果你想直接打电话,你应该使用requestPermissions方法。此行添加到您的清单文件:2.定义在活动类的类变量:private static Intent phoneCallIntent; //If use don't need a member variable is good to use a static variable for memory p
长时间过去了。但可能会帮助别人。 如果你想直接打电话,你应该使用requestPermissions方法。
此行添加到您的清单文件:
2.定义在活动类的类变量:
private static Intent phoneCallIntent; //If use don't need a member variable is good to use a static variable for memory performance.
添加这些行到onCreate活动方法:
final String permissionToCall = Manifest.permission.CALL_PHONE;
//Assume that you have a phone icon.
(findViewById(R.id.menuBarPhone)).setOnClickListener(new OnClickListener(){
public void onClick(View view) {
phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(getString(R.string.callNumber))); //Uri.parse("tel:your number")
if (ActivityCompat.checkSelfPermission(MainFrame.this, permissionToCall) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainFrame.this, new String[]{permissionToCall}, 1);
return;
}
startActivity(phoneCallIntent);
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 1){
final int permissionsLength = permissions.length;
for (int i = 0; i < permissionsLength; i++) {
if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
startActivity(phoneCallIntent);
}
}
}
当用户赋予的权限,下一次就不会有对话框和呼叫将被直接作出。
更多推荐
所有评论(0)