Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect screen unlock with React Native?

Tags:

react-native

Does anyone know a way in which I can detect when the user has opened their phone? To my understanding android.intent.USER_PRESENT is broadcast when the device is unlocked (e.g. correct password entered), however, I do not know how to detect the broadcast in with React native. Does anyone have a solution?

like image 513
HSonghurst Avatar asked Jul 14 '17 18:07

HSonghurst


People also ask

How does react native detect lock screen?

React Native device info provide isPinOrFingerprintSet() function to detect pin, password, pattern, and fingerprint lock set or not it will return only true or false . isPinOrFingerprintSet() function work in ios, android, and windows platform.

How do I know if an app is closed in react native?

With @react-native-community/hooks you can use the useAppState hook to check the app state. When you close the app, it gets a state of unknown once you open it back. When is in background, it says background' or 'inactive'.

What is AppState api?

AppState can tell you if the app is in the foreground or background, and notify you when the state changes. AppState is frequently used to determine the intent and proper behavior when handling push notifications.

What is screen unlock?

You can set up a screen lock to help secure your Android phone or tablet. Each time you turn on your device or wake up the screen, you'll be asked to unlock your device, usually with a PIN, pattern, or password. On some devices, you can unlock with your fingerprint.


1 Answers

Look into AppState API in FB(Face Book)

It has 3 states on FB and 5 total. I only see 3 in FB but the other 2 may or may not be suppprted.

Active - The app is running in the foreground

background - The app is running in the background. The user is either in another app or on the home screen.

inactive - This is a state that occurs when transitioning between foreground & background, and during periods of inactivity such as entering the Multitasking view or in the event of an incoming call

Check out apples Docs for more on these.

You're going to have to test what state is hit when you the phone is in the lockscreen. I can't tell you what state to use because I have never tested the api out.

as you can see from the code below the test is done in a conditional statement

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

I'm taking facebooks example here but attach the change event listiner in componentDidMount and remove in ComponentWillUnmount and the code will run accourding to the state.

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>
    );
  }

}
like image 133
Train Avatar answered Oct 02 '22 17:10

Train