I'm making an Android app with incoming calls. Is there any way in my app, to know if user rejected any incoming call before answering it?
-
Possible duplicate of [How to detect incoming calls, in an Android device?](https://stackoverflow.com/questions/15563921/how-to-detect-incoming-calls-in-an-android-device) – Nikola Feb 21 '18 at 14:03
2 Answers
First, you have to add a permission in manifest file.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Then you have to register a broadcast receiver in the manifest file.
<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Then write in your broadcast receiver's onReceive method:
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
//If the phone is **Ringing**
}else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){
//If the call is **Received**
}else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
//If the call is **Dropped** or **Rejected**
}
}
If the state changes from TelephonyManager.EXTRA_STATE_RINGING
to TelephonyManager.EXTRA_STATE_IDLE
, then it will be a missed call.
Like this you have to check the conditions.
Please put a log in those conditions. Then see the logs. I think you will get your answer.

- 8,890
- 6
- 44
- 59
-
I've already tried it. But I want to know is there any way to distinguish between call Dropped and Call Rejected ? – EvilGuy May 01 '17 at 00:38
-
@EvilGuy you have to check the conditions of the phone. I am updating the answer. Please check it. – Avijit Karmakar May 01 '17 at 02:30
-
I'm sorry if you didn't get my previous comment. I want to detect whether it was a missed call or user rejected the call before answering it. Because in both the cases, the Log in last "if-else" is printed. – EvilGuy May 01 '17 at 18:16
It's been a while since you asked, but I think it might be useful for future reference for others.
If you extract data from Android's calllog (e.g to XML or in your case to a variable in your app) you have two fields of interest:
1) numbertype e.g 1 for Incoming, 2 for Outgoing, 3 for missed
2) duration (in seconds)
Rejected number is (as you correctly mentioned) treated as numbertype=1. However if you combine numbertype=1 AND duration=0 (since any call answered will have duration>0) then this hopefully solves your problem.
I'm using Android 6, not sure if the types changed since then, but with me the above method works 99.9% of the time. Never managed to hang up the phone in less than a second :)

- 61
- 2