How can I detect screen unlock using React Native?

Does anyone know how I can detect when a user opened his phone? To my understanding, android.intent.USER_PRESENT is transmitted when the device is unlocked (for example, the correct password), however I do not know how to detect the broadcast using React native. Anyone have a solution?

+4
source share
1 answer

Look at the AppState API in FB (Face Book)

He has 3 states on FB and 5 points. I see only 3 in FB, but the other 2 may or may not be excluded.

Active - the application runs in the foreground

background- The application runs in the background. The user is either in another application or on the main screen.

inactive - , , ,

Docs, .

, , . , , api.

,

 if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') 

Facebooks , change listiner componentDidMount <26 > accourding .

import React, {Component} from 'react'
import {AppState, Text} from 'react-native'

class AppStateExample extends Component {

  state = {
    appState: AppState.currentState
  }

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
  }

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

  _handleAppStateChange = (nextAppState) => {
    if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
      console.log('App has come to the foreground!')
    }
    this.setState({appState: nextAppState});
  }

  render() {
    return (
      <Text>Current state is: {this.state.appState}</Text>
    );
  }

}
+4

Source: https://habr.com/ru/post/1681482/


All Articles