2017-07-14 390 views
4

有谁知道用户何时可以检测到用户打开手机的方式?据我了解,android.intent.USER_PRESENT在设备解锁时播放(例如输入正确的密码),但是,我不知道如何使用React native检测广播。有没有人有办法解决吗?如何使用React Native检测屏幕解锁?

+1

我不知道还有一座桥,但是如果发射了一个事件,你可以听它 –

回答

4

查找到AppState API in FB(Face Book)

它有FB 3国和5个总。我在FB中只看到3个,但其他2个可能或不可能被支持。

Active - 应用程序在前台

background运行 - 应用程序在后台运行。用户要么在另一个应用程序中,要么在主屏幕上。

inactive - 这是前景&背景之间转变时出现的状态,并且在非活动期间,如在进入多任务视图或呼入

退房苹果Docs的事件更多关于这些。

当手机处于锁屏状态时,您将不得不测试打到的状态。我无法告诉你使用什么状态,因为我从未测试过api。

,你可以从测试下面的代码看到的是在条件语句来完成

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

我走Facebook的例子在这里,但附加change事件listiner在componentDidMountComponentWillUnmount捞出代码将投奔国家。

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

}