简体   繁体   English

BroadcastReceiver和PhoneStateListener侦听呼叫之间的区别

[英]Difference between a BroadcastReceiver and a PhoneStateListener to listen to calls

I know, this may seem alike a "trivial" question, but there is a bit of confusion since there are multiple options available. 我知道,这似乎是一个“琐碎的”问题,但是由于存在多个可用选项,因此存在一些混乱。

I want to listen to phone calls and have found two solutions for doing the same: 我想听电话,并找到了两种解决方法:

These seem to be mutually exclusive. 这些似乎是互斥的。 What solution should I prefer? 我应该选择哪种解决方案? Do they really cater to different use cases? 他们真的满足不同的用例吗? If so, what might they be? 如果是这样,它们可能是什么?

If you use a BroadcastReceiver your application does not need to be running. 如果使用BroadcastReceiver则无需运行应用程序。 Android broadcasts call-related events and your BroadcastReceiver will be notified of call-related events and you can do whatever you want to do. Android广播与电话相关的事件,并且将向您的BroadcastReceiver通知与电话相关的事件,您可以执行任何想做的事情。

If you use a PhoneStateListener , your application must be running. 如果使用PhoneStateListener ,则您的应用程序必须正在运行。 You will need an Activity or a Service that creates the PhoneStateListener and registers it. 您将需要一个ActivityService来创建PhoneStateListener并进行注册。 Your PhoneStateListener will then be notified of call-related events. 然后,将向您的PhoneStateListener通知与呼叫相关的事件。 When your app is finished or killed by Android you will no longer receive any call-related events. 当您的应用程序被Android完成或终止后,您将不再收到任何与电话相关的事件。

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

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