Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a call directly?

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?

like image 270
Ronit Avatar asked Jan 14 '23 13:01

Ronit


2 Answers

It's not possible. This is for user protection.

like image 66
Nickolai Astashonok Avatar answered Jan 16 '23 02:01

Nickolai Astashonok


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.

like image 44
Arash Avatar answered Jan 16 '23 03:01

Arash