I am working with an android application which reads the phone states of incoming phone calls. And i have successfully captured the ringing,off-hook and idle states. But the problem is that i need to get the Call Waiting state of a new incoming call. When I tried to catch the state of the new call it always giving the same state OFFHOOK for both answering and hangup. Is there any way to distinguish the scenario.??
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CallListening";
int prev_state=0;
int call_num = 0;
private Context context;
public static String incoming_nr = null;
public static Boolean dialog = false;
public static Boolean new_call_ring=false;
public static Boolean cut = false;
public static String first_number ;
public CustomPhoneStateListener(Context context){
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber){
if(incomingNumber!=null&&incomingNumber.length()>0){
if(incoming_nr == null){
incoming_nr=incomingNumber;
first_number = incomingNumber;
call_num=1;
}
else
incoming_nr = incomingNumber;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "CALL_STATE_RINGING ==>" + incoming_nr);
prev_state = state;
if(!(first_number.equals(incoming_nr))){
if(!cut){
Log.d(TAG,"new call ringing "+incoming_nr);
new_call_ring = true;
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "CALL_STATE_OFFHOOK ==>" + incoming_nr);
if(!(first_number.equals(incoming_nr))){
if(new_call_ring=true){
Log.d(TAG,"new call answered or hangup");
}
}
prev_state = state;
break;
case TelephonyManager.CALL_STATE_IDLE:
cut = true;
Log.d(TAG, "CALL_STATE_IDLE==>" + incoming_nr);
// NumberDatabase database=new NumberDatabase(mContext);
if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) {
prev_state = state;
//Answered Call which is ended
Log.d(TAG, "The call is answered :" + incoming_nr);
}