长时间过去了。但可能会帮助别人。 如果你想直接打电话,你应该使用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);

}

}

}

当用户赋予的权限,下一次就不会有对话框和呼叫将被直接作出。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐