Detecting incoming and outgoing calls in Android

by Krishnaraj Varma on August 29, 2010

Download source code of this article

In one of my project, I wanted to detect incoming and outgoing calls. I used BroadcastReceiver with action android.intent.action.NEW_OUTGOING_CALL and android.intent.action.PHONE_STATE.

The android.intent.action.NEW_OUTGOING_CALL will be broadcasted when an outgoing call is initiated. The receiving intent will have an extra string variable Intent.EXTRA_PHONE_NUMBER which contains the outgoing number. This requires the permission android.permission.PROCESS_OUTGOING_CALLS.

To detect the incoming call, we register a BroadcastReceiver for the action android.intent.action.PHONE_STATE. This will be broadcasted when there is a change in phone state. The receiving intent will have an extra string variable TelephonyManager.EXTRA_STATE which describes the phone state. If this state is TelephonyManager.EXTRA_STATE_RINGING then there will be another extra string variable TelephonyManager.EXTRA_INCOMING_NUMBER. This variable contains the incoming phone number. Note that this variable will not be present when the state is not TelephonyManager.EXTRA_STATE_RINGING.

The sample application is a simple one just for demonstration. The application registers for these actions and when the calls are detected it just displays a toast.

{ 12 comments… read them below or add one }

Avinash July 7, 2011 at 1:33 pm

Mr.Varma, I found your blog when I was searching for TelephonyManager tutorial, it is one of the most clean, neat and useful article I have come across.

Kudos to you for taking this initiative of sharing your knowledge.

I have one more question can you suggest me how to pick incoming calls?say I have detected the call.

Reply

Abhin Chhabra July 15, 2011 at 4:40 pm

Wow man! This is so useful for what I’m doing. Thanks a lot buddy!

Reply

LL August 5, 2011 at 1:31 pm

hi i would like to create a incoming call recorder. The purpose of this is that when you are busy or in a meeting and you can’t answer the call, your phone will just record the call and you can answer it later. Can you give me link or idea where to start because im not yet familiar with android

Reply

kerul September 5, 2011 at 6:59 am

Nice tutorial. TQ for sharing this…

Reply

Deepsan September 13, 2011 at 8:26 am

Hello sir , very nice tutorial..
sir i made a receiver which open a particular activty on dialing of specific number.but i have a problems.

I dont want to press green button to open activity, it should be open autometic when we dial a specific no.
how can capture the dialing no, is there any event which can capture this no…
please hep its very urgent and needfulll.
Thanks..

Reply

Imani Emma September 14, 2011 at 4:14 am

Hey Mr. Varma, the world needs people like you…People who learn things and share that stuff with others. Thanx, this site is so helpful for me and many others out there, I guess..Yeah buddy, go on..Be blessed!

Reply

Vipul September 14, 2011 at 12:54 pm

Hi,

It is simply awesome!!!!!
Thanks for such a wonderful tutorial!!!
Keep The Good Work

Reply

kamal September 15, 2011 at 4:41 am

How can we detect the incoming call is accepted and the outgoing call has been accepted.

Reply

Marek September 26, 2011 at 8:50 am

Hi mate,
I’ve got a question to you about catching dial when I press on phone number in e-mail message. I want to swap default client to mine but I’m not sure what intent-filters I should use.Any suggestions ?

Reply

Tabish November 3, 2011 at 6:03 am

Hi ,

Please tell me how to disconnect call by programing .

Reply

Chris Breece December 1, 2011 at 4:18 pm

Thank you, this was very helpful to me.

Reply

Aki143S December 19, 2011 at 8:52 pm

Thanks a lot. Very neat and accurate.

Reply

Leave a Comment

 

Previous post:

Next post: