1

Java 开发者在 2.3 之前一直使用反射来达到 ITelephony 的 endcall 方法,以结束来电,但后来该方法被阻止,因此在 monodroid 中也无法通过 c# 访问。

有没有办法在“Mono For Android”中做到这一点?

4

1 回答 1

7

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);
于 2013-07-09T02:26:47.027 回答