简体   繁体   English

如何在Android中识别来电的开始和结束?

[英]How to identify the begin and end of an incoming call in Android?

I'm developing an application which should: 我正在开发的应用程序应:

  • Start when incoming call begins. 当来电开始时开始。
  • End as soon as the call has ended. 通话结束后立即结束。

More specifically, I don't want the application to begin while the phone is ringing, but when the actual conversation is begins (aka, the user has accepted the incoming call). 更具体地说,我不希望该应用程序在电话响起时启动,而是在实际对话开始时(又名,用户已接受来电)。

Does anyone know how to do this? 有谁知道如何做到这一点? And how to catch these events? 以及如何捕捉这些事件?

Broadcast Receiver will help you. 广播接收器将为您提供帮助。 Here 's the official google developer page on it. 是上面的Google开发人员官方页面。

Here is a small tutorial on the same. 是一个相同的小教程。

What you want is provided by the TelephonyManager . TelephonyManager提供所需的内容。

You should create a BroadcastReceiver which listens for ACTION_PHONE_STATE_CHANGED . 您应该创建一个BroadcastAction来监听ACTION_PHONE_STATE_CHANGED When you declare it in your AndroidManifest.xml it will spawn your service/activity. 当您在AndroidManifest.xml中声明它时,它将产生您的服务/活动。 When you are spawned it's very likely that the phone is currently just in the ringing state. 生成手机时,很有可能手机当前仅处于振铃状态。 So you have to register for further changes of the PhoneState via TelephonyManager.listen() . 因此,您必须通过TelephonyManager.listen()注册PhoneState的进一步更改。 This way you will notice when the phone got hooked off and also when it goes back to idle. 这样,您会注意到手机何时挂断以及何时返回空闲状态。

Don't forget to request the READ_PHONE_STATE permission . 不要忘记请求READ_PHONE_STATE权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM