问题
Concept:
My application needs to record user activities on phone. Activities contain Foreground, Background, Kill, Lock and Unlock.
What I did:
I am able to record application's Foreground, Background and Kill states using AppState library.
Requirement:
I need to perform actions on device Lock/Unlock states. Unfortunately I am unable to listen device lock and unlock event in above library.
Any help would be appreciated.
回答1:
Since that day I couldn't find any way to detect Lock/Unlock event in React native.
Solution:
- We've created sample modules in Android and iOS separate (Native code to get Lock/Unlock events)
- Using Android studio / XCode we've exported as React native modules
- Imported these modules in React native
It works fine for us.
Hope this would help you.
回答2:
Your app is pretty much a sandbox within your device. Listening to unlock states is not tied to anything within your app.
Your next best hope is to do something like this, but I would not recommend it as it most likely violates private API's (This is iOS only): https://stackoverflow.com/a/14271705/1363779
来源:https://stackoverflow.com/questions/45735832/react-native-how-to-detect-device-lock-unlock-event