1

Java developers had used reflection for reaching endcall method of ITelephony until 2.3, in order to end incoming call, but this method has been prevented later, so it is nonaccessible via c# in monodroid neither.

Is there any way to do it in 'Mono For Android'?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mir Kar
  • 15
  • 4

1 Answers1

7

Java developers had used reflection

It's the-same-just-different: instead of Java reflection, you'd use JNIEnv.

Assume you want to port this Java reflection-based code:

try {
    TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    Class c = Class.forName(manager.getClass().getName());
    Method m = c.getDeclaredMethod("getITelephony");
    m.setAccessible(true);
    ITelephony telephony = (ITelephony)m.invoke(manager);
    telephony.endCall();
} catch(Exception e){
    Log.d("",e.getMessage());
}

If you squint just right, you can get this (entirely untested!) C# code:

var manager = (TelephonyManager) this.GetSystemService (Context.TelephonyService); 

IntPtr TelephonyManager_getITelephony = JNIEnv.GetMethodID (
        manager.Class.Handle,
        "getITelephony",
        "()Lcom/android/internal/telephony/ITelephony;");

IntPtr telephony          = JNIEnv.CallObjectMethod (manager.Handle, TelephonyManager_getITelephony);
IntPtr ITelephony_class   = JNIEnv.GetObjectClass (telephony);
IntPtr ITelephony_endCall = JNIEnv.GetMethodID (
        ITelephony_class,
        "endCall",
        "()Z");
JNIEnv.CallBooleanMethod (telephony, ITelephony_endCall);
JNIEnv.DeleteLocalRef (telephony);
JNIEnv.DeleteLocalRef (ITelephony_class);
Community
  • 1
  • 1
jonp
  • 13,512
  • 5
  • 45
  • 60
  • thank you, i was working on JNIEnv lastly -after asking this question- however i had skipped calling getITelephony, and when i saw your answer i thought that had to be why i couldn't have accessed endCall... unfortunately it still gives "no such method error" for endcall line... best regards. – Mir Kar Jul 11 '13 at 12:31
  • I've also added [ITelephony.java](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/com/android/internal/telephony/ITelephony.java) as AndroidJavaSource... didn't help neither – Mir Kar Jul 11 '13 at 13:02
  • oh OK... got it... the problem is with the type reference. endCall is ()Z not (V)Z and also we must use CallBooleanMethod... – Mir Kar Jul 11 '13 at 13:28
  • tried and true... edited your answer a bit and now it works, thank you so so much, you can't guess how a big favor this is ! – Mir Kar Jul 11 '13 at 13:43
  • I am getting permission denied error. Can someone please help? – RL89 Feb 25 '20 at 12:53