Making and Receiving Phone Calls Android

It might come as a surprise to the younger generation (they usually just text), but phones are often still used for making and receiving phone calls. Any application can be made to initiate calls and answer incoming calls; however, these abilities should be used judiciously so as not to unnecessarily disrupt the calling functionality of the user’s device.

Making Phone Calls

You’ve seen how to find out if the handset is ringing. Now let’s look at how to enable your application to make phone calls as well.

Building on the previous example, which sent and received SMS messages, we now walk through similar functionality that adds a call button to the screen to call the phone number instead of messaging it.

The Android SDK enables phone numbers to be passed to the dialer in two different ways. The first way is to launch the dialer with a phone number already entered. The user then needs to press the Send button to actually initiate the call. This method does not require any specific permissions. The second way is to actually place the call. This method requires the android.permission.CALL_PHONE permission to be added to the application’s AndroidManifest.xml file.

Let’s look at an example of how to enable an application to take input in the form of a phone number and launch the Phone dialer after the user presses a button, as shown in Figure.

The user can enter a phone number in the EditText control and press the Call button to initiate a phone call from within the application.

The user can enter a phone number in the EditText control and press the Call button to initiate a phone call from within the application

We extract the phone number the user entered in the EditText field (or the most recently received SMS when continuing with the previous example).The following code demonstrates how to launch the dialer after the user presses the Call button:

Button call = (Button) findViewById(R.id.call_button);
call.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri number = Uri.parse(“tel:” +
numberEntry.getText().toString());
Intent dial = new Intent(
Intent.ACTION_DIAL, number);
startActivity(dial);
}
});

First, the phone number is requested from the EditText and tel: is prepended to it, making it a valid Uri for the Intent. Then, a new Intent is created with Intent .ACTION _DIAL to launch in to the dialer with the number dialed in already. You can also use Intent.ACTION_VIEW, which functions the same. Replacing it with Intent .ACTION_CALL, however, immediately calls the number entered. This is generally not recommended; otherwise, calls might be made by mistake. Finally, the startActivity() method is called to launch the dialer, as shown in Figure.

One emulator calling the other after the Call button is pressed within the application.

One emulator calling the other after the Call button is pressed within the application

Receiving Phone Calls

Much like applications can receive and process incoming SMS messages, an application can register to answer incoming phone calls. To enable this within an application, you must implement a broadcast receiver to process intents with the action Intent.ACTION_ANSWER.

Remember, too, that if you’re not interested in the call itself, but information about the incoming call, you might want to consider using the CallLog.Calls content provider (android.provider.CallLog) instead. You can use the CallLog.calls class to determine recent call information, such as

  • Who called
  • When they called
  • Whether it was an incoming or outgoing call
  • Whether or not anyone answered
  • The duration of the call

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics