【问题标题】:How to End Incoming Call in Monodroid?如何在 Monodroid 中结束来电?
【发布时间】:2013-07-02 13:40:38
【问题描述】:

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

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

【问题讨论】:

    标签: c# android xamarin.android phone-call telephonymanager


    【解决方案1】:

    Java 开发人员使用过反射

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

    假设你想移植这个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());
    }
    

    如果你眯着眼睛正好,你可以得到这个(完全未经测试!)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);
    

    【讨论】:

    • 谢谢,我终于在 JNIEnv 上工作了——在问了这个问题之后——但是我跳过了调用 getITelephony,当我看到你的回答时,我认为这就是我无法访问 endCall 的原因...不幸的是,它仍然为 endcall 行提供“没有这样的方法错误”...最好的问候。
    • 我还添加了 ITelephony.java 作为 AndroidJavaSource... 也没有帮助
    • 哦好的...明白了...问题出在类型引用上。 endCall 是 ()Z 而不是 (V)Z 而且我们必须使用 CallBooleanMethod...
    • 久经考验...稍微编辑了您的答案,现在它可以工作了,非常感谢,您无法猜到这是多么大的恩惠!
    • 我收到权限被拒绝错误。有人可以帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2018-05-23
    • 2014-01-24
    • 1970-01-01
    相关资源
    最近更新 更多