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
Thank You!
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 !
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)
Oh, i were missing the activity i didn’t createt this time … thank you!
and, if i want send parameters from broadcastreceiver to an service?
Then you have to pass service reference as a dependency to this BroadcastReceiver
Any idea how to do this? :( help me!
It is common issue. Google for it:)
Thanks
Nice tut :-) But how to know when call is ended (both incoming/outgoing)
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 :)
Got it… I used TelephonyManager.ACTION_PHONE_STATE_CHANGED Intent filter. :-)
great!
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?
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 :)
but how to call this outgoing receiver?
how to call? it will be called by outgoing call intent. intent is thrown by android system
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.
handle TelephonyManager.ACTION_PHONE_STATE_CHANGED Intent to detect call end
That’s it. Thanks
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…
What do you mean by ‘cut’?
now i am unable to make any outgoing call….
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
Why are you using smsManager to handle calls
in my app i want to send outgoing call number so i am using smsmanager
Send me your full code via facebook or linked in or email
what is your email id
Jacekmilewskik on gmail
Have you got my mail??
Jacek… She she meant to say “Call Ends” :-D
Using services how can i make a call to a specific contact number?
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.
Had the same problem, Solved this by adding the permission test like this:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
}
Thanks for your reply here. I was missing app permissions only.
hello
i want to detect each n every incoming n outgoing details from my phone, for that m able to detect the notifications from the “notificaiton bar” of my phone i.e. whatever comes in my “notification bar” m able to detect that data but if its not there in notification bar m not able to detect it.
plz help me to find the solution…
it should record each n every incoming n outgoing notification (calls, SMS, whatsapp, Facebook and other applications data).