1

I am trying to use BroadcastReceiver to receive incoming calls. I want to register call when the call is ended. Below code gives each state (idle,ringing n offhook) more than once with the same number (its not an issue of past call). Is it because phonestatelistner is called within the service.

 public class IncomingCallReceiver extends BroadcastReceiver {

 int stateString =0;
 int prevStat = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();

            if(null == bundle)
                    return;

            String state = bundle.getString(TelephonyManager.EXTRA_STATE);

            if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
            {
                    String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

                   String info = "Detect Calls sample application\nIncoming number: " + phonenumber;

                    Toast.makeText(context, info, Toast.LENGTH_LONG).show();
            }

            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            //CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();

            PhoneStateListener listener = new PhoneStateListener() {

            @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                  String p = incomingNumber;
                  switch (state) {
                  case TelephonyManager.CALL_STATE_IDLE:
                    prevStat = stateString;
                    stateString = 1;
                    Log.i("ON CALL STATE CHANGED","IDLE");
                    break;
                  case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.i("ON CALL STATE CHANGED","OFFFHOOK");
                    prevStat = stateString;
                    stateString = 2;
                    break;
                  case TelephonyManager.CALL_STATE_RINGING:
                    prevStat = stateString;
                    stateString = 3;
                    Log.i("ON CALL STATE CHANGED","RINGING");
                    break;
                  }

                  if(prevStat == 2 && stateString == 1)
                  {
                           Log.i("REGISTER A CALLL","==" +p);
                  }

            }   
            };

               telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
     }

  }

I also tried the same thing with phonestatelistener in a separate class, but it was not being called, i.e my code didn't reach there. In my manifest file I have registered receiver, but there is no declaration of phonestatelistener. Is that to be registered?

I am sorry if the solution is quite obvious but am new to android things. Any help is appreciated :)

Swati
  • 153
  • 1
  • 5
  • 10
  • Check this answer: http://stackoverflow.com/a/5949116/438466 – Rabi Dec 14 '11 at 08:57
  • Make sure you've opened your app after you installed it. Apps are in a stopped state until the user explicitly opens the app. See this: http://stackoverflow.com/a/9955247/786462 This can make it look like your BroadcastReceiver isn't being called. – DataDino Aug 09 '14 at 05:27

1 Answers1

0

Yes, choose listener or received , not both of them

Nico
  • 19
  • 2