Click here to Skip to main content
15,916,945 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
C#
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
            Log.d(TAG,"PhoneStateReceiver **" + ex.toString());
            return false;
        }
        return true;
    }


My c# interpretation
C#
public static bool KillCall(Context c)
        {
            try
            {
                TelephonyManager tm = (TelephonyManager)c.GetSystemService(Context.TelephonyService);
                Type tp = tm.GetType();
                System.Reflection.MethodInfo mi = tp.GetMethod("getITelephony", System.Reflection.BindingFlags.NonPublic);

                Type telephonyInter = mi.Invoke(tm, null).GetType().GetInterfaces().Single(e => e.Name == "ITelephony");
                System.Reflection.MethodInfo mo = telephonyInter.GetMethod("endCall");
                mo.Invoke(telephonyInter, null);
            }
            catch (Exception ex)
            {
                Toast.MakeText(c, ex.Message, ToastLength.Long).Show();
                return false;
            }
            return true;
        }
Posted
Comments
Mehdi Gholam 22-Dec-15 0:08am    
Run the codes with tests to see if the results match.
Jethro Daniel 22-Dec-15 13:18pm    
Yea,i would have done that but i need an android phone to test it
i dont have one at the moment so my whole development is based on a theory

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900