user173488
user173488

Reputation: 947

android make a miscall from my application

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.

Upvotes: 0

Views: 838

Answers (2)

Mehul Joisar
Mehul Joisar

Reputation: 15358

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 !!

Upvotes: 1

D&#39;yer Mak&#39;er
D&#39;yer Mak&#39;er

Reputation: 1632

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

Upvotes: 1

Related Questions