Reputation: 1662
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.?
Upvotes: 1
Views: 1378
Reputation: 2783
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;
}
}
}
Upvotes: 3
Reputation: 49
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;
}
}
}
Upvotes: 1