来电事件,而第二收入

incoming call event while second incomes

我有一个应用程序可以在来电结束时执行一些操作 我有一个 phone 状态侦听器,带有两个标志,仅检测已应答的收入呼叫并将 phone 号码存储到静态变量中,以便在呼叫结束时使用 当 phone 响铃时一个标志仅检测收入,当呼叫应答时另一个标志只为应答

做事

一切正常,但我发现如果在通话期间有另一个来电,那么即使拒绝它,我的应用程序也会为第二收入做一些事情

这是我的代码

public class ServiceReceiver extends BroadcastReceiver {

    static boolean is_incoming = false;
    static boolean in_call = false; //when incoming call ends check that call was answered
    public static String saved_number; //when call answers store number to use when ends
    static Context context;


    @Override
    public void onReceive(Context context, Intent intent) {
        ServiceReceiver.context = context;
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){ //when phone rings
            is_incoming = true;
            Toast.makeText(context, "ringing", Toast.LENGTH_SHORT).show();
        }
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){ //when incoming call answer
            in_call = true;
            saved_number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Toast.makeText(context, "offhook", Toast.LENGTH_SHORT).show();
        }
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){ // when reject or miss or ends


            if(in_call && is_incoming) { //answered incoming call ends

               doStuff(saved_number);
            }
            //making flags ready for next use
            in_call = false;
            is_incoming = false; 
        }

    }
}

我发现当第二个来电来电时,第二个 phone 号码会发送带有振铃状态的广播,但是当拒绝时 android 会发送带有第二个号码的摘机广播!

我自己的想法是如果in_call已经为真,则进入摘机状态什么都不做(不要更改已保存的号码) 所以我把摘机声明改成了这个

if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){ //when incoming call answer

            if(in_call){
                Toast.makeText(context, "inside checking", Toast.LENGTH_SHORT).show();
                //return;
            }

            saved_number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            //Toast.makeText(context, "offhook for " + saved_number, Toast.LENGTH_SHORT).show();
            in_call = true;
        }

但我发现 "inside checking" toast 每次都显示,我使用 !in_call 而不是 in_call 并且神奇地没有任何改变 如果取消注释 return 应用程序崩溃

现在我很困惑为什么!!!

请帮忙

幸运的是我可以解决问题 我只是将保存号码移至振铃语句(并从摘机中删除)并仅在当前未通话时保存号码 最终代码:

public class ServiceReceiver extends BroadcastReceiver {

    static boolean is_incoming = false;
    static boolean in_call = false; //when incoming call ends check that call was answered
    public static String saved_number; //when call answers store number to use when ends
    static Context context;


    @Override
    public void onReceive(Context context, Intent intent) {
        ServiceReceiver.context = context;
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){ //when phone rings
            is_incoming = true;
            if(!in_call){
                saved_number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Toast.makeText(context, "ringing by: " + saved_number, Toast.LENGTH_SHORT).show();
            }
        }
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){ //when incoming call answer
            in_call = true;
            Toast.makeText(context, "offhook", Toast.LENGTH_SHORT).show();
        }
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){ // when reject or miss or ends


            if(in_call && is_incoming) { //answered incoming call ends

               doStuff(saved_number);
            }
            //making flags ready for next use
            in_call = false;
            is_incoming = false; 
        }

    }
}

可能导致摘机检查出现问题的唯一原因是当呼叫接受时摘机接收不止一次!