java 如何检测和管理来电(Android)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6610750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:36:32  来源:igfitidea点击:

How to detect and manage incoming call (Android)?

javaandroidincoming-call

提问by Mr.James

I want to create an application that can detect incoming calls and start my custom activity after a certain number of beeps (rings), I mean after 2 or 3 or 5 beeps (rings) my activityis triggered. How can I do it?

我想创建一个应用程序,可以检测来电并在一定数量的哔哔声(响铃)后启动我的自定义活动,我的意思是在 2 或 3 或 5 次哔声(响铃)后我activity被触发。我该怎么做?

Thanks

谢谢

回答by Matthew Quiros

I don't think you can count the number of rings the phone made since the start of the incoming call. There can't be a definitive measure of a single ring because the user can easily change the ringtone to a non-repetitive tune, for example, a song.

我认为您无法计算自来电开始以来电话响铃的次数。由于用户可以轻松地将铃声更改为不重复的曲调,例如一首歌,因此无法对单个铃声进行明确的衡量。

What you can do, however, is count the amount of time that passed since the arrival of the call. Set up a BroadcastReceiverfor PHONE_STATE(you will need the corresponding permission in the manifest to receive the event). Once you receive the EXTRA_STATE_RINGINGthat came with the PHONE_STATE, set an alarm via the AlarmManagerthat will fire a Servicethat checks if EXTRA_STATE_OFFHOOK(broadcast when the call is picked up) has been broadcast after your waiting time. If not, then you can start your answering machine.

但是,您可以做的是计算呼叫到达后经过的时间。设置一个BroadcastReceiverfor PHONE_STATE(您将需要清单中的相应权限才能接收事件)。一旦您收到EXTRA_STATE_RINGING随附的PHONE_STATE,设置一个警报,该警报AlarmManager将触发一个Service检查EXTRA_STATE_OFFHOOK(在接听电话时广播)是否在您的等待时间后广播。如果没有,那么您可以启动您的答录机。

I have written a quick tutorial in my websiteon how to catch the call's arrival (when the phone rings), when the call is picked up, and when it ends.

在我的网站上编写了一个快速教程,介绍如何接听电话的到达(电话响起时)、何时接听电话以及何时结束。

回答by Андрей Москвичёв

My article about detecting incoming and outgoing calls, with the step-by-step instructions: Detecting incoming and outgoing phone calls on Android

我的关于检测来电和去电的文章,附有分步说明: 检测 Android 上的来电和去电

When you detect incoming call, you can start a timer, with interval equal to beepInterval * beepCount. And launch activity on this timer.

当您检测到来电时,您可以启动一个定时器,其间隔等于 beepInterval * beepCount。并在此计时器上启动活动。