1

I am catching the android.intent.action.PHONE_STATE change event by using following code snippet with android BroadCastReceiver. I could recognize the event successfully and handle the incoming calls also can be done by following code.

My requirement is identifying calls which are coming form a particular number, end that call through the code and invoke a web service for that incoming event.

In here this code runs twice for call receiving and ending events.

I want to stop calling this method for twice. How can i stop calling that code snippet for the second event (ending the call). It would be great-full if any one can help me on this.

 if (intent.getAction().equals(
            "android.intent.action.PHONE_STATE")) {
        Log.i("INFO", "Call Received");

        try {

            TelephonyManager telephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            Bundle bundle = intent.getExtras();

            Log.i("Calling database", "Creatinng DataHandler Object");
            DatabaseHandler db = new DatabaseHandler(context);
            String phoneNumber =db.getContact().getPhoneNumber();

            Log.i("Retriving : ", "Retriving .."+ db.getContact().getPhoneNumber());


            if ((bundle.getString("incoming_number").equals(phoneNumber))) {

                Log.i("Test Call", "This is the test call");

                @SuppressWarnings("rawtypes")
                Class c = Class.forName(telephony.getClass().getName());
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);

                ITelephony telephonyService = (ITelephony) m
                        .invoke(telephony);
                telephonyService = (ITelephony) m.invoke(telephony);


                telephonyService.endCall();

                // Call the web service and send log details

                ServiceClient sc = new ServiceClient();
                sc.serviceCall(context);


            } else {
                Log.i("Normal Call", "This is not the test call");
            }

        } catch (Exception e) {
            Log.i("Exception Occured", "Exception in call code snipet");
        }
    }

}

Ashoka
  • 41
  • 1
  • 1
  • 5

4 Answers4

4

I believe the issue you are having is that the phone state changes twice, once when the phone rings, then, after you end the call, when the phone goes idle.

You only want this code to run once so the best way to do that is to add an additional check of the phone state to see if it is ringing, if the phone is ringing then end the call.

This can be accomplished by adding the following the check:

if (intent.getAction().equals(
        "android.intent.action.PHONE_STATE")) {
    Log.i("INFO", "Call Received");

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//your code here that starts with try block
}

Hope that helps

maddesa
  • 1,014
  • 7
  • 18
0

you can try doing something like this as soon as the receiver starts:

    Bundle bundle = nIntent.getExtras();
    String phoneNr= bundle.getString("incoming_number");

    if(null == phoneNr)
    {   // this is outgoing call so bail out
        return;
    }
Evgeny Erlihman
  • 458
  • 4
  • 12
0

You cannot terminate incoming call using third party application due to android security reason. Check in "android application development cookbook" page number "164 "

vaibhav
  • 41
  • 1
  • 1
  • 4
0

I know it's too late but I found a batter solution for this. This normally happens on 5.0 and 5.1. You cannot stop it from calling twice but you can have a universal check to do all your work at one point. And this solution universally work on all OS. Here is the code

public void onReceive(Context context, Intent intent) {
    Object obj = intent.getExtras().get("subscription");
      long subId;
      if(obj == null) {
        subId = Long.MIN_VALUE; // subscription not in extras
      } else {
        subId = Long.valueOf(obj.toString()); // subscription is long or int
      }

      if(subId < Integer.MAX_VALUE) {
        // hurray, this is called only once on all operating system versions!
      }}

for more info check this link.

gingo
  • 3,149
  • 1
  • 23
  • 32
Nouman Ghaffar
  • 3,780
  • 1
  • 29
  • 37