如何在Android中挂断电话? [英] How to hang up outgoing call in Android?

查看:536
本文介绍了如何在Android中挂断电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中我们需要做的一件事情是控制拨出电话,至少可以阻止它从我们的应用程序中停止.

I am developing an application where one of the things we need is to control the outgoing call, at least to be able to stop it from our application.

我尝试从现有活动中使用Intent.ACTION_CALL:

I've tried using Intent.ACTION_CALL from an existing activity:

Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); 
startActivity(callIntent); 

但是通过API似乎不允许停止调用.

But stopping the call seems to be disallowed through the API.

您能建议一些解决方法吗?

Can you suggest some workaround?

例如:在通话中启用飞行模式?只是一个例子;此hack对我不起作用.

For example: enabling airplane mode during the call? Just an example; this hack didn't work for me.

推荐答案

已经提到了在BroadcastReceiver中捕获去电,如果要在拨号前结束通话,绝对是最好的方法.

Capturing the outgoing call in a BroadcastReceiver has been mentioned and is definitely the best way to do it if you want to end the call before dialing.

但是,一旦拨号或通话中,该技术将不再起作用.到目前为止,挂断电话的唯一方法是通过Java Reflection 进行.由于它不是公共API的一部分,因此您应该小心使用它,而不要依赖它. Android内部组成的任何更改都将有效破坏您的应用程序.

Once dialing or in-call, however, that technique no longer works. The only way to hang up that I've encountered so far, is to do so through Java Reflection. As it is not part of the public API, you should be careful to use it, and not rely upon it. Any change to the internal composition of Android will effectively break your application.

普拉桑塔·保罗(Prasanta Paul)的博客演示了如何实现该目标,我总结如下.

Prasanta Paul's blog demonstrates how it can be accomplished, which I have summarized below.

获取ITelephony对象:

TelephonyManager tm = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
try {
    // Java reflection to gain access to TelephonyManager's
    // ITelephony getter
    Log.v(TAG, "Get getTeleService...");
    Class c = Class.forName(tm.getClass().getName());
    Method m = c.getDeclaredMethod("getITelephony");
    m.setAccessible(true);
    com.android.internal.telephony.ITelephony telephonyService =
            (ITelephony) m.invoke(tm);
} catch (Exception e) {
    e.printStackTrace();
    Log.e(TAG,
            "FATAL ERROR: could not connect to telephony subsystem");
    Log.e(TAG, "Exception object: " + e);
}

结束通话:

telephonyService.endCall();

这篇关于如何在Android中挂断电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆