如何在Monodroid中结束来电

本文关键字:结束 来电 Monodroid | 更新日期: 2023-09-27 18:20:52

Java开发人员在2.3之前一直使用反射来到达ITelephony的endcall方法,以结束传入调用,但该方法后来被阻止了,因此它也不能通过monodroid中的c#访问。

有没有办法在"Android版Mono"中做到这一点?

如何在Monodroid中结束来电

Java开发人员使用了反射

它是相同的,只是不同:您将使用JNIEnv而不是Java反射。

假设您想要移植这个基于Java反射的代码:

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());
}

如果你斜视刚好,你可以得到这个(完全未经测试!)C#代码:

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);