Main Tutorials

How to make a phone call in Android

In this tutorial, we show you how to make a phone call in Android,and monitor the phone call states via PhoneStateListener.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1 Android Layout Files

Simpel layout file, to display a button.

File : res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/buttonCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="call 0377778888" />
            
</LinearLayout>

2. Activity

Use below code snippet to make a phone call in Android.


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

File : MainActivity.java – When the button is call, make a phone to 0377778888.


package com.mkyong.android;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.buttonCall);

		// add button listener
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

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

			}

		});

	}

}

3 Android Manifest

To make a phone call, Android need CALL_PHONE permission.


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

File : AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="10" />

	<uses-permission android:name="android.permission.CALL_PHONE" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
  
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4. PhoneStateListener example

Ok, now we update the above activity, to monitor the phone call states, when a phone call is ended, come back to the original activity (actually, it just restart the activity). Read comment, it should be self-explanatory.

Note
Run it and refer to the logcat console to understand how PhoneStateListener works.

File : MainActivity.java


package com.mkyong.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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	final Context context = this;
	private Button button;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.buttonCall);

		// add PhoneStateListener
		PhoneCallListener phoneListener = new PhoneCallListener();
		TelephonyManager telephonyManager = (TelephonyManager) this
			.getSystemService(Context.TELEPHONY_SERVICE);
		telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

		// add button listener
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

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

			}

		});

	}

	//monitor phone call activities
	private class PhoneCallListener extends PhoneStateListener {

		private boolean isPhoneCalling = false;

		String LOG_TAG = "LOGGING 123";

		@Override
		public void onCallStateChanged(int state, String incomingNumber) {

			if (TelephonyManager.CALL_STATE_RINGING == state) {
				// phone ringing
				Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
			}

			if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
				// active
				Log.i(LOG_TAG, "OFFHOOK");

				isPhoneCalling = true;
			}

			if (TelephonyManager.CALL_STATE_IDLE == state) {
				// run when class initial and phone call ended, 
				// need detect flag from CALL_STATE_OFFHOOK
				Log.i(LOG_TAG, "IDLE");

				if (isPhoneCalling) {

					Log.i(LOG_TAG, "restart app");

					// restart app
					Intent i = getBaseContext().getPackageManager()
						.getLaunchIntentForPackage(
							getBaseContext().getPackageName());
					i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(i);

					isPhoneCalling = false;
				}

			}
		}
	}

}

Update Android Manifest file again, PhoneStateListener need READ_PHONE_STATE permission.


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

File : AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
  
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

5. Demo

Activity started, just display a button.

android phone call example

When button is clicked, make a phone call to 0377778888.

android phone call example

When phone call is hang out or ended, restart the main activity.

android phone call example

Download Source Code

Download it – Android-Make-Phone-Call-Example.zip (16 KB)

References

  1. Android PhoneStateListener Javadoc
  2. Android TelephonyManager Javadoc
  3. Android Intent Javadoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
71 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
tien mau
10 years ago

Thank you very much.

ismail fedakar
5 years ago

Hello there.
Call Voice Changer
how to write a program like. Can You Share a Simple Example.

FAISAL
5 years ago

dear MKYONG..
i want to Ask about How to create merge calls in android..

Suri
7 years ago

For Android 6.0 Marshmallow it’s not working …
what are the things which i have to add in coding for Android 6.0 Marshmallow….

Apollo
7 years ago

how to do that (make a phone call ) when we have a recyclerview ???

Crystal
7 years ago
Reply to  Apollo

You have to make the button available in each item of the recycler view by inflating it each time in the adapter… Then give the button you’ve inflated an onClick listener… It automatically sets it for each item that passes through the adapter

Mithun
7 years ago

hi is there are way to make a video call by editing this code?

Chirag thaker
8 years ago

error coming in calling ,unfortunately stopped. running app from Android Lolipop Mobile

Freddy Roy Ebsan
8 years ago

THX. Its Work..

Ragib Ahsan
8 years ago

how can i add two phone call this program

android
9 years ago

Good one for sharing you knowledge with us.. can we make an app, so that once we will call from the native dialer then the request number should come into our application and then on the basis of number what its .will give a response and then dial that number.??

Rohit
9 years ago

Hi,

Thanks very nice example ,

in add on this how to perform like in this example we have fixed number and we are doing call on that,
other then that if we like to join some bridges or other calls which require some input like give your conference id and conference password to join bridge
so if we know these details and rather then putting that again and again for each call we save this by some name like canada_bridge as a contact
and do a call on that

Thanks

amit
9 years ago

disable the forwarding call

yahya
9 years ago

Thank You…

prabhakarn
10 years ago

ji i want to make a Internet call using sip api in android pls teach and give the whole code

Sam
10 years ago

Thanks mkyong, Very helpful.

Wanted to know if there is a way to make the loudspeaker ON at deafult.

jim
10 years ago

Great example. Unfortunately, on making the phone call, we always switch to the actual built -in phone application.
I want to avoid this, or at the very least hide the dialer pad button. The user SHOULD NOT have the option to enter a phone#.
Do you know of a way to achieve this? i.e. keep the actual built-in phone application in the background (I would need to add buttons for speaker, and end call in the primary application)

nishan
10 years ago

thanks

lampe torche
10 years ago

This app is for android phone only

Kushal Desai
10 years ago

Thank You. It worked.

ghouse
10 years ago

hai i have used your code and it working properly but i have one doubt in your app you r restarting app when call ends here in my case i am having two activities and it goes to launcher activity(activity 1) but i want to go activity 2 after call ends any suggestion from you waiting for replay

kenyan kid
10 years ago
Reply to  ghouse

hi, im also having the same problem, in case you figured it out, please help me.

Socheat
10 years ago

Hello! I’m Socheat.
I live in Cambodia.
I’m student.
I want to ask you. I write the code follow your code is complete but when I run this code on my android why it always show (Unfortunately, has stop) please answer my question.
Tank you!

jk
10 years ago

Hi
Can you please let me know how can i get numbersfrom int array to call.like i got a list of names and numbers so when i click on listview item,it calls the number stored in that position in an array

Roberto
10 years ago

Simple, Objective , Effective, thank you very much.

saeed
10 years ago

the best ever
very very helpful
thanks!!!!!!!!!!!

Sanjay
10 years ago

Very nice Example very very help fully………….

Anchit
10 years ago

Thanks. Really a nice and helpful tutorial

iphone apps development
10 years ago

After going over a few of the blog posts on your web
site, I seriously appreciate your way of blogging. I saved it to my bookmark site list and will be checking back in the near future.

Please visit my web site too and let me know what you think.

ankush
10 years ago

hi.

app is working fine.i have put eg “111155” number to call and after “end call” its comes to main activity which i want.

but for any other call eg “65464646” when i end it call my apps main activity even after exiting the app before i dialed 65464646..

ankush
10 years ago
Reply to  ankush

for any subsequent end call it calls my app only ..but when i end my app from apps setting ie force close ..it does not call my app..

kanzo
10 years ago

awesome tutorial, I really like your website.

I have a question about the code below, it should be create a new activity but restart activity,right?

//restart app
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

raul
10 years ago

Could we have a tutorial about google cloud messages in adroind please?