[英]How to detect a phone call in Android phone with React Native?
I'm building an app and I need to detect all phone calls, assuming my app is always in the background.我正在构建一个应用程序,我需要检测所有电话,假设我的应用程序始终在后台。
I've looked over react-native-call-detection
package from https://www.npmjs.com/package/react-native-call-detection .我从https://www.npmjs.com/package/react-native-call-detection查看了
react-native-call-detection
包。
The question is, how do I make the listener global , meaning that it doesn't matter in which screen the user is or if the app is in background, how to detect incoming calls?问题是,我如何使侦听器成为 global ,这意味着用户在哪个屏幕或应用程序是否在后台无关紧要,如何检测来电?
You could create a new class for the listener, than make a new class element in every screen.您可以为侦听器创建一个新类,而不是在每个屏幕中创建一个新类元素。 For example you create a class like, phoneCallListener, this class has a constructor, and a function which starts the detection, which name would be, startDetection();
例如,您创建了一个像 phoneCallListener 这样的类,这个类有一个构造函数和一个启动检测的函数,它的名字是 startDetection(); Then in every activity you create a new element, like phoneCallListener myListener = new phoneCallListener();
然后在每个活动中创建一个新元素,例如 phoneCallListener myListener = new phoneCallListener(); , then you call the method and you are read, myListener.startDetection();
,然后调用该方法并读取, myListener.startDetection();
Another soluton is to make only one screen with fragments, so you will have one activity which's screen can change, so there will be a global class, and a fragment class.另一种解决方案是仅使用片段制作一个屏幕,因此您将有一个屏幕可以更改的活动,因此将有一个全局类和一个片段类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.