How to make a call programmatically in android

Ram Ramesh

I want to make/receive calls periodically for testing purposes from my android app programmatically and collects stats from my network. So my app will call a number every so often and when the call is answered the app will terminate the call after a few seconds. TO begin with here is the code I understood would work. It will dial and a call the number I specify without me having to touch the screen.

public class MainActivity extends AppCompatActivity {

int MY_PERMISSIONS_REQUEST_CALL_PHONE = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    call();
}

private void call() {

    try {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:2125551212"));
        System.out.println("====before startActivity====");



        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) !=
                PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);

            return;
        }

        startActivity(callIntent);
        System.out.println("=====getcallActivity==="+getCallingActivity());


    } catch (ActivityNotFoundException e) {
        Log.e("helloAndroid","Call failed",e);
    }
}

}

The manifest has this line:

As per my understanding ACTION_CALL should place the call to the number I have provided without having to press the DIAL button. But is acting like ACTION_DIAL, which displays the number on the screen and the user then has to press DIAL button to place the call. So is there no difference between ACTION_DIAL and ACTION_CALL?

After reading some of the posts I understand that 6.0 onwards permission is needs to be requested from the user to dial the call(which I have used in my code above)

My question is if I use Lollipop(5.0) OS then will I be able to place the call without dialing?

Charuක

ACTION_DIAL

Added in API level 1

String ACTION_DIAL

Activity Action: Dial a number as specified by the data. This shows a UI with the number being dialed, allowing the user to explicitly initiate the call.

Input: If nothing, an empty dialer is started, else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.

Output: nothing.

Constant Value: android.intent.action.DIAL


ACTION_CALL

Added in API level 1

String ACTION_CALL

Activity Action: Perform a call to someone specified by the data.

Input: If nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: URI of an explicit phone number.

Output: nothing.

Note:

  • There will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.
  • This Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL.
  • If you app targets android M and above and declares as using the CALL_PHONE permission which is not granted, then attempting to use this action will result in a SecurityException.

Constant Value: android.intent.action.CALL


so basically

To just open the dialer app (the user has to press the call button inside the dialer app; no additional permissions needed) use:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

To open the dialer app and do the call automatically (needs android.permission.CALL_PHONE) and then use:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_CALL, call); 
startActivity(surf); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make horizontal line in Android programmatically

Android M Light and Dark status bar programmatically - how to make it dark again?

How to turn on speaker for incoming call programmatically in Android L?

Make a phone call programmatically

How to make a phone call programmatically?

How to make a phone call using intent in Android?

How to make a TextView multiline programmatically in Android (Java)

How to call a View Controller programmatically?

How to make text glow programmatically (via code, not xml) in android?

Programmatically disconnect call in android Marshmallow version

How to make a drawable Shape programmatically (Android)

Programmatically make phone call crash in swift 3

Make a call on Android 10

Get incoming call number programmatically on android 10

How to make a special call and get result in android?

How to make buttons scroll-able in Android, programmatically?

How to make incoming call in Genymotion emulator for Android?

Can I hang up a call programmatically in android?

How can I display a warning information "missed call" in notification bar when I hang up a call programmatically in android?

How do I make a secure HTTPS call from Android

how to programmatically call the "click" of css

Make a call in android

How to write a phone number for calling in android but not make the call

Android : simulate / mimic a call programmatically on device

How to make a long shadow programmatically

How can i make a call on wildcard numbers in my android application?

How to make call to my mobile number in Android sip stack SipManager?

How can I convert an XML to make a programmatically view in Android Java

How to make the Java to call a String from a XML file(In Android Studio)