Android Make Phone Call Example

0

In this article, I will show you how to make a phone call.

Android provides two different ways to make a call.

The first one allows you to make a call directly. You need to explicitly set the phone number beforehand.

The second method allows you to dial a phone number and then the user has to explicitly initiate the call.

Next, I will also show you how to monitor the call as it progress through different states from Idle, to ringing, to Off-hook and back to Idle.

Main Screen

welcome.xml:

Our main screen contains a text field where the user can enter the phone number. It has two buttons, one to make a call and another to dial. The difference between call and dial is, dial takes you to a different screen where you will initiate the call. You can even re-enter the phone number before making the call.
The inputType of the text field is set to phone. Both buttons have onClick attributes set. One will call the call() method and the other the dial() method.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.javarticles.android.DatePickerExample" >

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="18dp"
        android:text="@string/welcome"
        android:textColor="@color/welcome_text_color"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/phone_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:hint="@string/enter_number"
        android:inputType="phone">

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/call_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/call"
        android:onClick="call"/>

    <Button
        android:id="@+id/dial_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="dial"
        android:text="@string/dial" />

</LinearLayout>

Telephony Permissions

We need permission android.permission.CALL_PHONE to make calls and to monitor the phone states, we need permission android.permission.READ_PHONE_STATE.

androidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javarticles.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:maxSdkVersion="22"
        android:minSdkVersion="14"
        android:targetSdkVersion="22" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher_sandbox"
        android:label="@string/app_name" >
        <activity android:name="com.javarticles.android.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Main Activity

Let’s now go through our MainActivity. The purpose of this call is two folds:

  1. Implement call() and dial() methods
  2. Monitor call states

In order to communicate with other services, you always needs an Intent object. Here we need to communicate with the telephony services.
In call() method, we create an intent of call action Intent.ACTION_CALL and then set its data to the telephone number you want to call. It should be in a URI form: tel:<your number>. Once Intent is created, we call startActivity(intent) to start the service.

dial() method is exactly same as call(), only the intent action changes. It is Intent.ACTION_DIAL for dial service.

Now let’s come to the monitoring part.

In order to monitor call states, we first need access to TelephonyManager which provides access to information about the telephony services.

TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 

Next, we call telephonyManager.listen and set PhoneStateListener object. We will monitor the call states in the PhoneStateListener. We will impkement our stuff in the callback method onCallStateChanged(int state, String incomingNumber). The state will let us know whether the call is ringing, or off-hook or idle. Once the user ends the call, we will get back to our main screen.

MainActivity:

package com.javarticles.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText phoneNumber;
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.welcome);
		phoneNumber = (EditText) findViewById(R.id.phone_number);
		
		MyPhoneStateListener phoneStateListener = new MyPhoneStateListener();
		TelephonyManager telephonyManager = 
			(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 
		telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
	}
	
	public void call(View view) {
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		Toast.makeText(this, "Call " + phoneNumber.getText().toString(), Toast.LENGTH_SHORT).show();
		callIntent.setData(Uri.parse("tel:" + phoneNumber.getText().toString()));
		startActivity(callIntent);
	}
	
	public void dial(View view) {
		Intent callIntent = new Intent(Intent.ACTION_DIAL);
		Toast.makeText(this, "Call " + phoneNumber.getText().toString(), Toast.LENGTH_SHORT).show();
		callIntent.setData(Uri.parse("tel:" + phoneNumber.getText().toString()));
		startActivity(callIntent);
	}
	
	private class MyPhoneStateListener extends PhoneStateListener {
		 
		private boolean callActive = false;
 
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
 
			switch (state) {
			case TelephonyManager.CALL_STATE_RINGING:
				Toast.makeText(MainActivity.this, incomingNumber + " ringing", 
						Toast.LENGTH_LONG).show();
				break;
			
			case TelephonyManager.CALL_STATE_OFFHOOK:
				Toast.makeText(MainActivity.this, "call offhook...", 
						Toast.LENGTH_LONG).show();
				callActive = true;
				break;

			case TelephonyManager.CALL_STATE_IDLE:
				if (callActive == true) {
					Toast.makeText(MainActivity.this, "call idle", 
							Toast.LENGTH_LONG).show();
 
					// restart our application
					Intent restart = getBaseContext().getPackageManager().
						getLaunchIntentForPackage(getBaseContext().getPackageName());
					restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(restart);
 
					callActive = false;
				}
				break;
			default:
				break;
			}
			
		}
	}

}

Run the application

This is the main screen. Here we will enter the number and either call or dial.

Call Idle

Starts with Call Idle

Let’s enter a number and call.

Make a call

Make a call

Call is going through.

Calling...

Calling…

Here we press dial instead of call.

Dial

Dial

Download the source code

This was an example of making phone calls in android. You can download the source code here: telephonyExample.zip

Share.

Comments are closed.