9

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ram Ramesh
  • 169
  • 1
  • 2
  • 9

2 Answers2

12

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); 
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Thank you very much. It works great! Should I be able to set this up as an AsyncTask? – Ram Ramesh Dec 18 '16 at 04:09
  • 1
    Not clear what you meant,AsyncTask is one of the easiest ways to implement parallelism in Android without having to deal with more complex methods like Threads, so if you want to perform some parallel work you can have it but to dial or call make sure you do it on the UI thread otherwise it will crash .use onPostExecute if you want to dial after some process in the background thread .. – Charuක Dec 18 '16 at 04:20
2

You can't actually make a direct call to a number unless you're a system application with special system permissions. Which requires you to be preinstalled by the OEM or be rooted and installed as a system app. THe intent you need to be using is android.intent.action.CALL_PRIVILEGED, and the permission is android.permission.CALL_PRIVILEGED

The difference between ACTION_CALL and ACTION_DIAL is that dial launches with the dialer up.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127