1

I have been browsing a lot of code examples and questions/answers for starting a phone call from my activity, however, i can not find a way to stop that call i made.

The purpose is to make a test call for 10 seconds for example and stop it.

Have someone done this before , any ideas ?

Anas
  • 713
  • 1
  • 8
  • 17

2 Answers2

1

As @milapTank guided me, to answer my question for someone else's use :

  • Download ITelephony.java file depending on android version.
  • create a package named : com.android.internal.telephony and put interface file in it.

The Code

 //iTelephony
    try {
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        Class cl = Class.forName(telephonyManager.getClass().getName());
        Method method = cl.getDeclaredMethod("getITelephony");
        method.setAccessible(true);
        ITelephony telephonyService = (ITelephony) Method.invoke(telephonyManager);
        telephonyService.endCall();

    } catch (Exception e) {
        //manage exception...
    }

It's working fine with - Android Studio 1.5 - Android 4.0 As minimum SDK and android 6.0 as Target and Compile SDK.

Anas
  • 713
  • 1
  • 8
  • 17
-1
button.setOnClickListener(this);
@Override
public void onClick(View view) {
    if(view == ok){
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + num));
        activity.startActivity(intent);

}

And must in the manifest:

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

..

EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    if(TelephonyManager.CALL_STATE_RINGING == state) {
        Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
    }
    if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
        //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
        Log.i(LOG_TAG, "OFFHOOK");
    }
    if(TelephonyManager.CALL_STATE_IDLE == state) {
        //when this state occurs, and your flag is set, restart your app
        Log.i(LOG_TAG, "IDLE");
    }
}
}

In the manifest :

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
johnrao07
  • 6,690
  • 4
  • 32
  • 55