Android BroadcastReceiver tutorial: detect outgoing phone call event

13 04 2013

Tutorial shows how to catch outgoing phone call event in Android application (including called phone number information).

This basically consists of catching an Intent that is sent by Android OS while call was initiated. So the BroadcastReceiver is required. Steps to build it are as follows:

1. Create OutgoingCallBroadcastReceiver

This is a BroadcastReceiver that will catch and handle incoming intents. Full implementation is short:

public class OutgoingCallReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString());
		Toast.makeText(context, "Outgoing call catched!", Toast.LENGTH_LONG).show();
		//TODO: Handle outgoing call event here
	}
}

2. Register OutgoingCallBroadcastReceiver in AndroidManifest.xml

Newly created BroadcastReceiver will wait for android.intent.action.NEW_OUTGOING_CALL intent actions. In order to direct such intents to Receiver add this code to AndroidManifest.xml:

<receiver android:name=".OutgoingCallReceiver" >
	<intent-filter>
		<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
	</intent-filter>
</receiver>

3. Add permission in AndroidManifest.xml

Your application will now need to request for PROCESS_OUTGOING_CALLS permission. Add that request to AndroidManifest.xml:

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

4. Get called phone number (optional)

Intent that I just catched has Extra with phone number, so if you need it, just get that String Extra from intent with:

String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

Just add the foregoing line to the OnReceive method:

@Override
public void onReceive(Context context, Intent intent) {
	String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
	Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString() + ", call to: " + phoneNumber);
	Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
	//TODO: Handle outgoing call event here
}

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!


Actions

Information

33 responses

11 07 2013
AntonioE.

That’s exactly what i need, thank you, but can i make a question? ( i’m new in android programming … ) how can i make this run on my phone? i mean, whit “standard” application i create a new android project in eclipse, i code a “main activity” that extends activity and then i run and close it. With a broadcast receiver instead? i tried the same way, but no toast appear when i make a call, what i’m missing?

Thank you for the eventual answer and sorry for my horrible english :P !

11 07 2013
Jacek Milewski

Yes – You shopuld do exactly what you described. Leave your activity. don’t delete it. just add another class – the BroadcastReceiver.

Remember also to edit AndroidManifest.xml to register Broadcast receiver and declare required permissions.

And that’s all I can do for you now – Since you are asking for Android basics, please find some starter tutorials (search for ‘Best Android tutorials’ post on my blog)

11 07 2013
AntonioE.

Oh, i were missing the activity i didn’t createt this time … thank you!

29 07 2013
Jerry Salazar

and, if i want send parameters from broadcastreceiver to an service?

29 07 2013
Jacek Milewski

Then you have to pass service reference as a dependency to this BroadcastReceiver

30 07 2013
Jerry Salazar

Any idea how to do this? :( help me!

30 07 2013
Jacek Milewski

It is common issue. Google for it:)

31 07 2013
Jerry Salazar

Thanks

31 08 2013
iOS tutorial: Send SMS programmatically | Looks OK!

[…] to use all phone features including sending SMS in background, listening for incoming SMS, calls, initiating calls etc. It trusts user that if he install app he agrees that it may use such features on his […]

24 09 2013
Anuj

Nice tut :-) But how to know when call is ended (both incoming/outgoing)

24 09 2013
Jacek Milewski

there should be another kind of intent idicating call end. I did not need to use it so I am not sure – google for it :)

26 09 2013
Anuj

Got it… I used TelephonyManager.ACTION_PHONE_STATE_CHANGED Intent filter. :-)

26 09 2013
Jacek Milewski

great!

24 09 2013
Anuj

I am working on Audio app. I want to pause music when phone rings or user dials a number, and resume when call ends.. How can I do it?

24 09 2013
Jacek Milewski

there should be another kind of intent idicating call end. I did not need to use it so I am not sure – google for it :)

29 09 2013
atef farouk

but how to call this outgoing receiver?

30 09 2013
Jacek Milewski

how to call? it will be called by outgoing call intent. intent is thrown by android system

30 09 2013
atef farouk

first, thanks for your reply,
my question is: i want to know when will the call finish to do something. your help is highly appreciated.

30 09 2013
Jacek Milewski

handle TelephonyManager.ACTION_PHONE_STATE_CHANGED Intent to detect call end

1 10 2013
atef farouk

That’s it. Thanks

19 12 2013
monika

i m using dis code…no i am able to get outgoing number..but problem is that when i m making a new outgoing call it cut automatically…

19 12 2013
Jacek Milewski

What do you mean by ‘cut’?

19 12 2013
monika

now i am unable to make any outgoing call….

19 12 2013
monika

if(intent.getAction().equals(“android.intent.action.NEW_OUTGOING_CALL”))
{
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
smsManager.sendTextMessage(s, null, “outgoing call to=”+number, null, null);
System.out.println(“outgoing call to”+ number);

}
i am doing like this

19 12 2013
Jacek Milewski

Why are you using smsManager to handle calls

19 12 2013
monika

in my app i want to send outgoing call number so i am using smsmanager

19 12 2013
Jacek Milewski

Send me your full code via facebook or linked in or email

19 12 2013
monika

what is your email id

19 12 2013
Jacek Milewski

Jacekmilewskik on gmail

19 12 2013
monika

Have you got my mail??

23 12 2013
Anuj Devasthali

Jacek… She she meant to say “Call Ends” :-D

24 10 2014
Anonymous

Using services how can i make a call to a specific contact number?

30 10 2015
M Chisty

This code does not work. Tested in Nexus 5, Nexus 6. I am just able to make outgoing calls. I set the debug point on OutgoingCallReceiver#onReceive(..) method to check, but the method is not even invoked. I set the permission to “android.permission.PROCESS_OUTGOING_CALLS”, set the intent filter to “android.intent.action.NEW_OUTGOING_CALL”, none of those works. Outgoing call is allowed.

It is not working mate.

Thanks.

Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: