How to make a call directly?

2019-06-26 07:15发布

问题:

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?

回答1:

See the answer

You will need add CALL_PHONE and CALL_PRIVILEGED permissions to manifest file.

Then the number can be called using:

Uri callUri = Uri.parse("tel://911");
Intent callIntent = new Intent(Intent.ACTION_CALL,callUri);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(callIntent);


回答2:

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



回答3:

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.



回答4:

Just change ACTION_DIAL to ACTION_CALL.
Like this:

Intent dialintnt = new Intent(Intent.ACTION_CALL,Uri.parse("tel:911"));
startActivityForResult(dialintnt, CALLING);


回答5:

Try this:

   Intent callIntent = new Intent(Intent.ACTION_CALL);
   callIntent.setData(Uri.parse("tel:" +phone_number));
   startActivity(callIntent);


回答6:

Number has to be proper formatted as if you try with country code it will take you default dailer. i tested it on my app when i am using number as 198 call is happening directly and when i am using 9999999999 this will invoke the dailer so that user can correct the number by adding country code.

`val intent =  Intent(Intent.ACTION_CALL, Uri.parse("tel:+91987654310" ))
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
      startActivity(intent)`