0

i need to make a phone call from my application to some predefined phone number, and after some time interval, lets say 10 seconds after it starts ringing, i want to automatically terminate this call, leaving a miscall. is this possible? i used the following code to start the phone call

   try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+phoneNumber);
            startActivity(callIntent);
        } catch (ActivityNotFoundException e) {
            Log.e("helloandroid dialing example", "Call failed", e);
        }

but i have no idea how to stop this particular call.

user173488
  • 947
  • 1
  • 9
  • 20

2 Answers2

1

After initiating the call successfully. you can always use endCall() for disconnecting it after your desired interval of time has passed.

Read this thread and search for similar ones on this site: https://stackoverflow.com/a/9987399/2021499

Community
  • 1
  • 1
D'yer Mak'er
  • 1,632
  • 5
  • 24
  • 47
1

First of all,you need to initiate a call and monitor its state and then upon particular time,end the call programmatically .

You already know how to initiate the call,I have added a PhoneStateListener to monitor its state,and I have also added a code snippet which describes how to end call programmatically.

initiate the call,

    private void establishCall()
{
            TelephonyManager tManager = (TelephonyManager) 
              getSystemService(Context.TELEPHONY_SERVICE);
    if (tManager .getSimState() != TelephonyManager.SIM_STATE_ABSENT)
    {                   

        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            callIntent.setData(Uri.parse("tel:"+phoneNumber));
            startActivity(callIntent);



            ListenToPhoneState listener = new ListenToPhoneState();
            tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        } catch (Exception e) {
            Log.e("Call failed", e.toString());
        }

    }
    else 
    {
        Toast.makeText(this, "Insert a Simcard into your device.", Toast.LENGTH_LONG).show();
    }
}

here is the code of listener,

        private class ListenToPhoneState extends PhoneStateListener {

        boolean callEnded=false;
        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                UTILS.Log_e("State changed: " , state+"Idle");

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                UTILS.Log_e("State changed: " , state+"Offhook");
                callEnded=true;
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                UTILS.Log_e("State changed: " , state+"Ringing");

        //apply your logic here to wait for few seconds before calling killCall(this) function to end call


                break;


            default:
                break;
            }
        }

    }

here is the code for ending current call

public boolean killCall(Context context) {
try {
   // Get the boring old TelephonyManager
   TelephonyManager telephonyManager =
      (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

   // Get the getITelephony() method
   Class classTelephony = Class.forName(telephonyManager.getClass().getName());
   Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

   // Ignore that the method is supposed to be private
   methodGetITelephony.setAccessible(true);

   // Invoke getITelephony() to get the ITelephony interface
   Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

   // Get the endCall method from ITelephony
   Class telephonyInterfaceClass =  
       Class.forName(telephonyInterface.getClass().getName());
   Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");


       // Invoke endCall()
       methodEndCall.invoke(telephonyInterface);

   } catch (Exception ex) { // Many things can go wrong with reflection calls
      Logger.e("PhoneStateReceiver **" + ex.toString());
      return false;
   }
   return true;
}

inside manifest following permissions will be required,

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />

I hope it will be helpful !!

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • hi, thx for the code, i tried it but unfortunately it is not working, the listener is never fired, have any idea why? – user173488 Mar 25 '13 at 13:59
  • after debugging i found that the problem is with the state sent to the listener, it is always 0! and onstatechanged is never called when state change to call_state_ringing... – user173488 Mar 25 '13 at 14:42