2

I use Broadcast receiver to catch Phone state Change. It works fine when the state change in in first time (for State_OffHook), but don't react when the call ends. This is my code:

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                     
            if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {working fine}
                else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {doesn't react}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
ItayM
  • 912
  • 2
  • 15
  • 36

1 Answers1

0

When you have no call you are in IDLE state and when you get a call it goes to OFFHOOK state and when your call ends it again goes to IDLE state

for more info refer this

How to know whether I am in a call on Android?

EDIT:

@Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                // Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
                if(UDF.phoneState != TelephonyManager.CALL_STATE_IDLE) {
                    //Here you are came from offhook because value of UDF.phoneState != TelephonyManager.CALL_STATE_IDLE
                    //IDLE is calls many times so you have to keep track by a static variable like  UDF.phoneState
                } 
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                 //Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                 //Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
                endCallIfBlocked(incomingNumber);
                break;

            default:
                break;
        }
        UDF.phoneState = state;
     }
Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • I know that, The problem is that I'm trying to make changes in the phone. when the state is OFF_HOOK its work, and when that phone change back to IDLE its don't work. I've tried to change the code to the one in the link that you sent but still i have the same problem. – ItayM Jul 18 '11 at 06:35
  • I have edited my answer and still if you have problem then tell me what exactly you want – Dharmendra Jul 18 '11 at 08:46
  • @Dharmendra, you just need to catch phone state in `BroadcastReceiver`. It's enough. Even you keep track of state in a listener, things still go wrong. I mean, do keep track as your way, but in `BroadcastReceiver`. Thank you so much :-) –  Feb 14 '12 at 21:14