I'm making an app which wants the user to keep the app open and not using other apps (to help them focuses on works), but allows them to turn the phone screen off. I used AppState API to detect if the app running in the background but it also triggered when the screen turned off. So I need a way to know if the screen is on or off. I tried googling around but I couldn't find any solution.
Asked
Active
Viewed 405 times
1 Answers
0
The short answer is that no RN library supports it for both platforms. For example, here you can find a solution for ios Detect screen on/off from iOS service and wrapper for RN react-native-lock-detection
However in your case, if we know that we receive a second event when the screen is off we can add a counter and when the app goes background the first time you increase the value if the second time you increase it again and add some logic. If the app goese active you reset this value.

Kirill Novikov
- 2,576
- 4
- 20
- 33
-
Thanks for your answer, can I call function when screen off using this implementation – Hitul Nayakpara Jan 11 '23 at 05:39
-
if you need to do something in background better to use https://github.com/Rapsssito/react-native-background-actions – Kirill Novikov Jan 11 '23 at 06:40