when I use this code, first comes the dial pad screen with this number.
Intent dialintnt = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:911"));
startActivityForResult(dialintnt, CALLING);
I don't want that screen. I want that when I click button directly calling that number.
So how can I call a number onclick
?
It's not possible. This is for user protection.
It's been a long time. But may help someone else. If you want to call directly, you should use requestPermissions method.
1. Add this line to your manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />
2. Define a class variable in the activity class:
private static Intent phoneCallIntent; //If use don't need a member variable is good to use a static variable for memory performance.
3. Add these lines to the onCreate method of the activity:
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);
}
});
4. And for making a call immediately after clicking on Allow button, override onRequestPermissionsResult method:
@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);
}
}
}
When a user give the permission, next time there will be no dialogue box and call will be make directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With