嘗試下面的代碼希望它會幫助你。 首先,在允許撥打電話號碼後,會要求您提供允許彈出窗口。
if (ContextCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(HomePanelActivity.this,
Manifest.permission.CALL_PHONE)) {
ActivityCompat.requestPermissions(HomePanelActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION);
}
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumberToCall));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
}
} else {
Snackbar.make(drawerLayout, "You Deny permission", Snackbar.LENGTH_SHORT).show();
return;
}
}
};
也許這將是有用的爲你http://stackoverflow.com/a/36787464/3436179 – Alexander
檢查這個裁判:http://www.tutorialspoint.com/android/android_phone_calls.htm –