以编程方式结束来电

24

这是 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/

在安卓系统中拒绝来电

如何在安卓4.1中编程地自动接听/结束电话?

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

如何在Android中屏蔽电话

如何在Android应用程序开发中屏蔽移动电话号码的呼叫和短信接收?

以及http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html和更多问题、答案和建议...

所有人都说要使用ITelephonyService.aidlTelephonyManager相结合,这个解决方案在许多设备上都运行得很完美,但在三星S Duos上却无法正常工作。我已经苦苦挣扎了一周,但仍未找到解决方案...是否有任何特殊的API可用于此类设备?我该如何拒绝来电?请帮帮我...

3个回答

66

试试这个,肯定有效。对我来说运行得很好。

您可以从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); 
 }
}

2
太棒了,运行得非常好!你节省了我很多时间,我很困惑为什么这篇帖子的投票如此之少。 - bgplaya
你可以捕获哪种异常?你试过在不同种类的设备上工作吗? - bgplaya
其实我想知道那些异常是与反射相关还是与系统服务相关? - bgplaya
2
@Jeba:我的问题是,它会断开哪个呼叫,只有来电还是其他呼叫也会被断开。如果在另一个呼叫正在进行时同时开始响铃,则会发生什么? - Narendra Singh
1
此函数在目标版本30中不可用。 - Ashish Virani
显示剩余12条评论

0

尝试使用这个函数:

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

-1
在您的应用程序中创建一个包(com.android.internal.telephony)在java文件夹中。然后将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" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接