2

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);
                        }
Cœur
  • 37,241
  • 25
  • 195
  • 267
ARUNBALAN NV
  • 1,634
  • 4
  • 17
  • 39

1 Answers1

0

i think i come late hear but i like to share my solution : Telephone manager does not Provide Direct 'Waiting' state in android There are main three states:

1)'IDLE' - when device in idle generally after hangup or if you not use phone

2)'RINGING' - ringing (when incoming call coming)

3)'OFF_HOOK' - The phone is off the hook Now you have to play between these three states:

My logic id when first call coming you pick and than second call come in live line so you have to check in Ringing state(because when incoming call come its detect by RINGING state) like below:

 case TelephonyManager.CALL_STATE_RINGING:
         isIncoming = true;
         incomingNumber1 = incomingNumber;
         if(lastState==TelephonyManager.CALL_STATE_OFFHOOK){
                //its in waiting state add your logic detect 2nd number hear
                Log.e("Hear","2nd call come ");
            } else {
                //not in waiting state only first call detect only line 1 in ringing
            }
         break;
Nikunj Peerbits
  • 785
  • 4
  • 12
Jayman Jani
  • 1,251
  • 10
  • 15