end incoming call programmatically
Asked Answered
P

3

24

This is a familiar question in SO. and what I need is to end call programmatically. I have searched a lot...

http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html http://androiddesk.wordpress.com/2012/08/02/blocking-a-call-without-user-intervention-in-android/

Rejecting Incoming call in android

How to programmatically answer/end a call in Android 4.1?

http://www.emoticode.net/android-sdk/block-incoming-and-outgoing-phone-calls-programmatically.html

How to block calls in android

how to block a mobile number call and message receiving in android application development?

and http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html

and lot more questions, answers and suggestions...

All are saying use ITelephonyService.aidl in combination with TelephonyManager

and the solution is working perfect on many devices but it's not working on Samsung S Duos. I am struggling over a week but didn't get a solution.. is there any special API to work with on this type of devices? How can I reject incoming call? please help me...

Peripheral answered 7/1, 2014 at 6:57 Comment(0)
T
66

Try this Sure it will work. It's working fine for me.

You could download the ITelephony.java file from ITelephony.java

After that you add the method to end call:

Function to Disconnect call

public void disconnectCall(){
 try {

    String serviceManagerName = "android.os.ServiceManager";
    String serviceManagerNativeName = "android.os.ServiceManagerNative";
    String telephonyName = "com.android.internal.telephony.ITelephony";
    Class<?> telephonyClass;
    Class<?> telephonyStubClass;
    Class<?> serviceManagerClass;
    Class<?> serviceManagerNativeClass;
    Method telephonyEndCall;
    Object telephonyObject;
    Object serviceManagerObject;
    telephonyClass = Class.forName(telephonyName);
    telephonyStubClass = telephonyClass.getClasses()[0];
    serviceManagerClass = Class.forName(serviceManagerName);
    serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
    Method getService = // getDefaults[29];
    serviceManagerClass.getMethod("getService", String.class);
    Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
    Binder tmpBinder = new Binder();
    tmpBinder.attachInterface(null, "fake");
    serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
    IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
    Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
    telephonyObject = serviceMethod.invoke(null, retbinder);
    telephonyEndCall = telephonyClass.getMethod("endCall");
    telephonyEndCall.invoke(telephonyObject);

  } catch (Exception e) {
    e.printStackTrace();
    Log.error(DialerActivity.this,
            "FATAL ERROR: could not connect to telephony subsystem");
    Log.error(DialerActivity.this, "Exception object: " + e); 
 }
}
Throw answered 9/1, 2014 at 7:16 Comment(17)
Awesome, working like a charm! You saved me a lot of time, I'm confused why this post have so less of votesGosh
What kind of exception you can catch there ? Did you try that working on different kinds of devices ?Gosh
Actually I wanna know if that exceptions connected with reflection or with system services ?Gosh
@GokulJai, Please let me know it throwing any error for you?. It's working fine for us.Throw
@Jeba , by using above code, call is disconnect after one ring. is there any posibility without single ring.?Scrawl
Where to call this function?Aboriginal
@NarendraDroidWorm, You can call the function in your button click functions.Throw
@Jeba: so, my question is - which call it will disconnect, the only incoming call or the others too. As, if the incoming call start ringing on the same time, when another call in progress. What will happen on calling this?Aboriginal
@NarendraDroidWorm u got ur answerGaucho
Unknown identifier: "com/android/internal/telephony/ITelephony.java". Falling back to searchTacky
I can confirm that this method works even on Android Lollipop (tested on 5.1.1). The "collateral" effect is that the user doesn't see the missed call notification, however it works great.Wizard
@Jeba Man you SAVED ME! Thanks a ton. this method working great even after enabling proguard and this was my big problem. thanks :)Staal
@Jeba will it end all types of calls ? or only the calls which user dialed ?Novelistic
and what kind of permissions are required for this to work ?Novelistic
@Jeba can you please write a step by step tutorial for this? Some how it doesn't work on my Sony Xperia C2305, JellyBean. :(Novelistic
Can you please explain why you need tmpBinder? The parameters null, "fake" look a little strangeHetman
this function not work in target version 30.Lovieloving
C
0

Try this function:

 private void endCall() {
        Context context = getApplicationContext();
        try {
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> c = Class.forName(telephony.getClass().getName());

            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);

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

            // Funciona en 2.2
            // Funciona en 2.3.3
            telephonyService.endCall();

            logManager.debug("ITelepony was used (endCall)");
        } catch (Exception e) {
            logManager.error("Error ending call: " + e.getMessage());
            logManager.debug("Error ending call", e);
        }
    }
Chancellorsville answered 1/7, 2014 at 8:23 Comment(0)
T
-1

In your Application create a package (com.android.internal.telephony) in java folder. And then copy ITelephone interface in that package

ITelephone interface source: http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/com/android/internal/telephony/ITelephony.java/?v=source

Code for Disconnect Call:

Context context = getApplicationContext();
try {
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    Class<?> c = Class.forName(telephony.getClass().getName());
    Method m = c.getDeclaredMethod("getITelephony");
    m.setAccessible(true);
    ITelephony telephonyService = (ITelephony) m.invoke(telephony);
    telephonyService.answerRingingCall();
} catch (Exception e) {
}

Also add permissions for using this methods:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
Tracee answered 19/4, 2016 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.