React Native: How to detect device lock/unlock event?

Deadly 提交于 2019-12-22 18:43:01

问题


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:

  1. We've created sample modules in Android and iOS separate (Native code to get Lock/Unlock events)
  2. Using Android studio / XCode we've exported as React native modules
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!