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,293 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
3 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,753
- 15
- 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
2
try this,
you should create broadcast receiver like this,
public class CallReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
// Phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Ringing state
// This code will execute when the phone has an incoming call
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is answered or disconnected
}
}
}
also add following in your manifest.xml file.
<receiver android:name=".CallReciever" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Ganpat Kaliya
- 1,234
- 2
- 9
- 16
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