TelephonyManager.CALL_STATE_IDLE calls more than one time

AndEngine picture AndEngine · Nov 29, 2012 · Viewed 7.2k times · Source

I have a class, a listener of Phone call state. in this, when the call is disconnected this line,

case TelephonyManager.CALL_STATE_IDLE:
Log.e(TAG, "CALL_STATE_IDLE");
break;

executes morethan one time. this affects my requirement badly. please help me if anybody knows this issue.

thanks in advance

 public class IncomingCallListener extends BroadcastReceiver
        {
            private Context mContext;
            private static final String TAG = "CustomBroadcastReceiver";
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            mContext = context;
            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            CustomPhoneStateListener customPhoneListener = new   CustomPhoneStateListener();
            telephony.listen(customPhoneListener,   PhoneStateListener.LISTEN_CALL_STATE);
            Bundle bundle = intent.getExtras();
            String phoneNr= bundle.getString("incoming_number");

        }
        public class CustomPhoneStateListener extends PhoneStateListener
            {
                private static final String TAG = "CustomPhoneStateListener";

                @Override
                public void onCallStateChanged(int state, String incomingNumber) 
                {
                switch (state) 
                {
                    case TelephonyManager.CALL_STATE_RINGING:
                        if(!incomingNumber.equalsIgnoreCase(""))
                        {
                            Log.e(TAG, "CALL_STATE_RINGING");
                        }
                        break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.e(TAG, "CALL_STATE_OFFHOOK");
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.e(TAG, "CALL_STATE_IDLE");
                    break;
                default:
                    break;
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        }       
    } 

Answer

ρяσѕρєя K picture ρяσѕρєя K · Nov 29, 2012

Change your Code as:

public class IncomingCallListener extends BroadcastReceiver
        {
            private Context mContext;

            private static final String TAG = "CustomBroadcastReceiver";
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            mContext = context;
            Bundle extras = intent.getExtras();
            if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.w("DEBUG", state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                CustomPhoneStateListener customPhoneListener = new   CustomPhoneStateListener();
                telephony.listen(customPhoneListener,   PhoneStateListener.LISTEN_CALL_STATE);
                Bundle bundle = intent.getExtras();
                String phoneNr= bundle.getString("incoming_number");
            }
            else{
                        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
                            {
                               // DO SOME CODE HERE...
                            }
                        else{
                        if (state.equals(TelephonyManager.CALL_STATE_OFFHOOK))
                            {
                               // DO SOME CODE HERE...
                            }                          
                        }
            }
        }

     }
//YOUR CODE HERE...