2017-07-14 388 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> 
    ); 
    } 

}