I found many questions those are describing how to get the call state of a phone call using telephonymanager .And I was following this question in stackoverflow - How to detect when phone is answered or rejected. But this is to detect the various phone states separately. I need to add a alert dialog only when a incoming call hang up with out answered. How to detect this particular event.?
Asked
Active
Viewed 1,342 times
1
-
I think the answer in that link is perfect for your situation. It sounds like you want to detect rejection of an incoming call – Tim Sep 22 '15 at 12:01
2 Answers
3
Android 5.0 and above may send duplicate events. You may consider keeping the previous state for comparison -
private class PhoneStateChangeListener extends PhoneStateListener {
static final int IDLE = 0;
static final int OFFHOOK = 1;
static final int RINGING = 2;
int lastState = IDLE;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
lastState = RINGING;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
lastState = OFFHOOK;
break;
case TelephonyManager.CALL_STATE_IDLE:
if (lastState == RINGING) {
// Process for call hangup without being answered
}
lastState = IDLE;
break;
}
}
}

headuck
- 2,763
- 16
- 19
-
my broadcast listener class can listen to the event while my app is not running? – ARUNBALAN NV Sep 22 '15 at 13:57
-
Yes, with a full setup like http://stackoverflow.com/questions/13395633/add-phonestatelistener – headuck Sep 22 '15 at 14:03
1
Try the below code:
private class PhoneStateChangeListener extends PhoneStateListener {
boolean isAnswered = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
isAnswered = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
isAnswered=true;
break;
case TelephonyManager.CALL_STATE_IDLE:
if(isAnswered == false)
//Call hangup without answered
break;
}
}
}

guna a
- 49
- 1
- 1
- 7
-
thanks you for the quick response. I have one more doubt that how to add Alert Dialog Box in above described code. It's required context parameter.And when i am trying to do with it Fatal error occured. – ARUNBALAN NV Sep 22 '15 at 13:55