【问题标题】:end incoming call programmatically以编程方式结束来电
【发布时间】:2014-01-24 18:51:28
【问题描述】:

这是 SO 中一个熟悉的问题。我需要的是 以编程方式结束通话。 我已经搜索了很多...

http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html http://androiddesk.wordpress.com/2012/08/02/blocking-a-call-without-user-intervention-in-android/

Rejecting Incoming call in android

How to programmatically answer/end a call in Android 4.1?

http://www.emoticode.net/android-sdk/block-incoming-and-outgoing-phone-calls-programmatically.html

How to block calls in android

how to block a mobile number call and message receiving in android application development?

http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html

还有更多问题、答案和建议...

所有人都在说将ITelephonyService.aidlTelephonyManager结合使用

该解决方案在许多设备上都运行良好,但在 Samsung S Duos 上却无法运行。我挣扎了一个多星期,但没有得到解决方案.. 在这种类型的设备上是否有任何特殊的 API 可以使用?如何拒接来电?请帮帮我...

【问题讨论】:

    标签: android telephony telephonymanager


    【解决方案1】:

    试试这个 肯定会奏效的。它对我来说很好。

    您可以从 ITelephony.java 下载 ITelephony.java 文件

    然后添加结束调用的方法:

    断开呼叫的功能

    public void disconnectCall(){
     try {
    
        String serviceManagerName = "android.os.ServiceManager";
        String serviceManagerNativeName = "android.os.ServiceManagerNative";
        String telephonyName = "com.android.internal.telephony.ITelephony";
        Class<?> telephonyClass;
        Class<?> telephonyStubClass;
        Class<?> serviceManagerClass;
        Class<?> serviceManagerNativeClass;
        Method telephonyEndCall;
        Object telephonyObject;
        Object serviceManagerObject;
        telephonyClass = Class.forName(telephonyName);
        telephonyStubClass = telephonyClass.getClasses()[0];
        serviceManagerClass = Class.forName(serviceManagerName);
        serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
        Method getService = // getDefaults[29];
        serviceManagerClass.getMethod("getService", String.class);
        Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
        Binder tmpBinder = new Binder();
        tmpBinder.attachInterface(null, "fake");
        serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
        IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
        Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
        telephonyObject = serviceMethod.invoke(null, retbinder);
        telephonyEndCall = telephonyClass.getMethod("endCall");
        telephonyEndCall.invoke(telephonyObject);
    
      } catch (Exception e) {
        e.printStackTrace();
        Log.error(DialerActivity.this,
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.error(DialerActivity.this, "Exception object: " + e); 
     }
    }
    

    【讨论】:

    • 太棒了,工作就像一个魅力!你为我节省了很多时间,我很困惑为什么这篇文章的票数这么少
    • 你可以在那里捕捉到什么样的异常?你试过在不同类型的设备上工作吗?
    • 其实我想知道异常是否与反射或系统服务有关?
    • @GokulJai,请让我知道它会为您抛出任何错误吗?对我们来说效果很好。
    • @Jeba:所以,我的问题是 - 哪个电话会断开,唯一的来电或其他来电。因为,如果来电同时开始响铃,则当另一个通话正在进行时。调用这个会发生什么?
    【解决方案2】:

    试试这个功能:

     private void endCall() {
            Context context = getApplicationContext();
            try {
                TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                Class<?> c = Class.forName(telephony.getClass().getName());
    
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);
    
                ITelephony telephonyService = (ITelephony) m.invoke(telephony);
    
                // Funciona en 2.2
                // Funciona en 2.3.3
                telephonyService.endCall();
    
                logManager.debug("ITelepony was used (endCall)");
            } catch (Exception e) {
                logManager.error("Error ending call: " + e.getMessage());
                logManager.debug("Error ending call", e);
            }
        }
    

    【讨论】:

      【解决方案3】:

      在您的应用程序中,在 java 文件夹中创建一个包 (com.android.internal.telephony)。然后复制那个包里的ITelephone接口

      ITelephone接口来源: http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/com/android/internal/telephony/ITelephony.java/?v=source

      断开呼叫代码:

      Context context = getApplicationContext();
      try {
          TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          Class<?> c = Class.forName(telephony.getClass().getName());
          Method m = c.getDeclaredMethod("getITelephony");
          m.setAccessible(true);
          ITelephony telephonyService = (ITelephony) m.invoke(telephony);
          telephonyService.answerRingingCall();
      } catch (Exception e) {
      }
      

      同时添加使用此方法的权限:

      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
      <uses-permission android:name="android.permission.CALL_PHONE" />
      <uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多