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 the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
Suri
Guest
Suri

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
Guest
Apollo

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

Crystal
Guest
Crystal

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
Guest
Mithun

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

Chirag thaker
Guest
Chirag thaker

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

Freddy Roy Ebsan
Guest
Freddy Roy Ebsan

THX. Its Work..

Ragib Ahsan
Guest
Ragib Ahsan

how can i add two phone call this program

android
Guest
android

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
Guest
Rohit

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
Guest
amit

disable the forwarding call

yahya
Guest
yahya

Thank You…

prabhakarn
Guest
prabhakarn

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

Sam
Guest
Sam

Thanks mkyong, Very helpful.

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

jim
Guest
jim

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
Guest
nishan

thanks

lampe torche
Guest
lampe torche

This app is for android phone only

Kushal Desai
Guest
Kushal Desai

Thank You. It worked.

ghouse
Guest
ghouse

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
Guest
kenyan kid

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

Socheat
Guest
Socheat

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!

tien mau
Guest
tien mau

Thank you very much.

jk
Guest
jk

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
Guest
Roberto

Simple, Objective , Effective, thank you very much.

saeed
Guest
saeed

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

Sanjay
Guest
Sanjay

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

Anchit
Guest
Anchit

Thanks. Really a nice and helpful tutorial

iphone apps development
Guest
iphone apps development

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
Guest
ankush

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
Guest
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
Guest
kanzo

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
Guest
raul

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

swati
Guest
swati

how can i change this number with some name like Customer Care?

kanzo
Guest
kanzo

String tmp = “tel:” + text; //text is the customer care number
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(tmp));
startActivity(callIntent);

Aldnav
Guest
Aldnav

Special case for multi sim/network phones.

What if I wanted to preset either Sim1 or Sim2. And I chose Sim1 as default for my application?

Because the above case applies seamlessly (which means that when I press the button it makes the call directly) but on multi sims, it visits the dialer first to choose one sim before you can successfully make a call.

Any help would be appreciated. Thanks!

Great tutorial!

Ali
Guest
Ali

i dont think that multi-sim Android devices exist :))))