0

I have created an application that calls a number. It is working but I want to close the calling after 30 sec. How can I do that? My code is:

     MyTimer = new CountDownTimer(60000, 30000) {

           public void onTick(long millisUntilFinished) {


                intent.setData(Uri.parse("tel:" + num2 + ""));
                startActivity(intent);


                            }

            public void onFinish() {


            }
        }.start();
duggu
  • 37,851
  • 12
  • 116
  • 113

1 Answers1

0

You'll need some Reflection for that. First, create this interface:

package com.android.internal.telephony;
public interface ITelephony {
    boolean endCall();
    void answerRingingCall();
    void silenceRinger();
}

And end call with this:

private void endCall() {
    TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
    Class<?> myClass = null;
    try {
        myClass = Class.forName(telephonyManager.getClass().getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    Method method = null;
    try {
        method = myClass.getDeclaredMethod("getITelephony");
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    method.setAccessible(true);
    ITelephony telephonyService = null;
    try {
        telephonyService = (ITelephony) method.invoke(telephonyManager);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    telephonyService.endCall();
}

Modified answer from here.

EDIT: Adding example:Add this to some button click

    //Start call
    makeCall(phoneNumber);
    //wait several seconds
    try {
        long time = holdCallSeconds * 1000;
        Thread.sleep(time);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    //hang up
    endCall();


void makeCall(String phoneNumber) {
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse(phoneNumber));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
    startActivity(intent);
}
Community
  • 1
  • 1
Marius
  • 810
  • 7
  • 20