Search code examples
androidbroadcastreceiverandroid-broadcasttelephonymanagerphone-state-listener

incoming call event while second incomes


I have an app to do some stuffs when incoming call ends I have a phone state listener with two flags to only detect answered income calls and store phone number into a static variable to use when call ends one flag when phone rings to detect income only and other flag when call answers to do stuffs only for answered

everything works proper but I find that if during a call have another incoming then my app do stuffs for second income even if reject it

here is my code

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

    }
}

I found that when second incoming call comes a broadcast send with ring state by second phone number but when rejects seems like android sends a offhook broadcast with second number!

the idea of my own is to inside offhook state if in_call is already true do nothing(don't change saved number) so I changed offhook statement to this

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

but I found that "inside checking" toast shows every time and I used !in_call instead of in_call and magically nothing changed and if uncomment return application crashes

now I'm so confused why!!!

please help


Solution

  • Fortunately I could fix issue I just moved saving number to ringing statement (and delete from offhook) and save number only if currently isn't in call final code:

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

    the only reason that can cause issue of checking this on offhook is that offhook is receiving more than one time when call accepts!