如何检测和管理来电(Android)?
我想创建一个应用程序,可以检测来电并在一定数量的蜂鸣声(响铃)后启动我的自定义活动,我的意思是在 2 或 3 或 5 声蜂鸣声(响铃)后我的 activity
被触发。我该怎么做呢?
谢谢
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 activity
is triggered. How can I do it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您无法计算自来电开始以来电话响了多少次。无法对单个铃声进行明确的测量,因为用户可以轻松地将铃声更改为不重复的曲调,例如歌曲。
然而,您可以做的是计算自呼叫到达以来所经过的时间。为
PHONE_STATE
设置一个BroadcastReceiver
(您需要清单中的相应权限才能接收事件)。收到PHONE_STATE
附带的EXTRA_STATE_RINGING
后,通过AlarmManager
设置闹钟,该闹钟将触发Service
检查EXTRA_STATE_OFFHOOK
(在接听电话时广播)是否在您的等待时间后广播。如果没有,那么您可以启动答录机。我写了一个快速教程在我的网站中,了解如何捕获呼叫的到达(当电话响起时)、呼叫何时接听以及呼叫何时结束。
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
BroadcastReceiver
forPHONE_STATE
(you will need the corresponding permission in the manifest to receive the event). Once you receive theEXTRA_STATE_RINGING
that came with thePHONE_STATE
, set an alarm via theAlarmManager
that will fire aService
that checks ifEXTRA_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.I have written a quick tutorial in my website on how to catch the call's arrival (when the phone rings), when the call is picked up, and when it ends.
我关于检测来电和去电的文章,包含分步说明:
在 Android 上检测来电和去电
当您检测到来电时,您可以启动一个计时器,间隔等于 beepInterval * beepCount。并在此计时器上启动活动。
My article about detecting incoming and outgoing calls, with the step-by-step instructions:
Detecting incoming and outgoing phone calls on Android
When you detect incoming call, you can start a timer, with interval equal to beepInterval * beepCount. And launch activity on this timer.