Android 中如何挂断去电?
我正在开发一个应用程序,其中我们需要的一件事是控制传出呼叫,至少能够从我们的应用程序中阻止它。
我尝试在现有活动中使用 Intent.ACTION_CALL
:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
但似乎不允许通过 API 停止调用。
你能建议一些解决方法吗?
例如:通话时启用飞行模式? 只是一个例子; 这个黑客对我不起作用。
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.
I've tried using Intent.ACTION_CALL
from an existing activity:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
But stopping the call seems to be disallowed through the API.
Can you suggest some workaround?
For example: enabling airplane mode during the call? Just an example; this hack didn't work for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
已经提到过在
BroadcastReceiver
中捕获拨出呼叫,如果您想在拨号之前结束呼叫,这绝对是最好的方法。然而,一旦拨号或通话,该技术就不再起作用。 到目前为止,我遇到的唯一挂断方法是通过 Java Reflection 来挂断。 由于它不是公共 API 的一部分,因此您应该小心使用它,而不是依赖它。 对 Android 内部组成的任何更改都会有效地破坏您的应用程序。
Prasanta Paul 的博客演示了如何实现这一点,我对此进行了总结以下。
获取
ITelephony
对象:结束通话:
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.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's blog demonstrates how it can be accomplished, which I have summarized below.
Obtaining the
ITelephony
object:Ending the call:
编辑:对于 Android P 或更高版本,请参阅:https://stackoverflow.com/a/51121175/ 450148
试试这个:(
我使用反射来访问高级电话功能 并修改一些东西)
EDIT: To Android P or newer, please see: https://stackoverflow.com/a/51121175/450148
Try this:
(I used Reflection to access advanced telephony features and modify somethings)
BroadcastReceiver
。onReceive
方法调用中拦截ACTION_NEW_OUTGOING_CALL
意图setResultData(null)< /code> 在同一方法中
这将阻止调用启动(只要您的接收者是最后一个处理我认为的意图)
BroadcastReceiver
with a priority of 0.ACTION_NEW_OUTGOING_CALL
intent in itsonReceive
methodsetResultData(null)
in the same methodThis will prevent the call from initiating (as long as your receiver is the last to process the intent I think)
这是最新的代码,它也适用于 Android P,因为它有一个官方 API (此处) :
在清单中,添加以下内容:
在代码中,使用以下内容:
Java:
或在 Kotlin 中:
Here's the most updated code, which will work for Android P too, because it has an official API for it (here) :
in manifest, add this:
In code, use this:
Java:
or in Kotlin:
您可以尝试启用然后禁用飞行模式:
You can try enabling then disabling airplane mode:
对于 Ilana:
此外,在清单中放入包部分:
仅此而已。
For Ilana:
In addition in Manifest put in package section:
That is all.
考虑到可能造成奇妙恶作剧的可能性,如果允许这样做,我会感到惊讶。
该帖子直截了当地指出API 无法结束调用。 其他人已经尝试过。
Considering the potential for wonderful mischief I would be surprised if this is allowed.
This thread says flatly that the API cannot end a call. Others have tried.
根据 ACTION_NEW_OUTGOING_CALL 的文档
According to the documentation on ACTION_NEW_OUTGOING_CALL