如何在通话期间获取新来电的呼叫等待状态

时间:2015-10-15 09:47:00

标签: android phone-call telephonymanager phone-state-listener busy-waiting

我正在使用一个Android应用程序,它可以读取来电的电话状态。我已经成功捕获了振铃,摘机和空闲状态。但问题是我需要获得来电的呼叫等待状态。 当我试图捕捉到新呼叫的状态时,它总是为应答挂断提供相同的状态 OFFHOOK 。 有没有办法区分情景。??

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);
                        }

1 个答案:

答案 0 :(得分:0)

我想我来晚听,但我想分享我的解决方案: 电话经理不提供直接等待'在android中的状态 主要有三种状态:

1)' IDLE' - 当设备处于空闲状态时,通常在挂断后或者您不使用电话时

2)' RINGING' - 响铃(来电时)

3)' OFF_HOOK' - 手机没电了 现在你必须在这三种状态之间进行游戏:

我的逻辑ID是第一次呼叫你选择而不是第二次呼叫进入实时线路,所以你必须检查振铃状态(因为当来电呼叫通过RINGING状态检测到时),如下所示:

 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;