加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 如何使用React Native检测屏幕解锁?

发布时间:2020-12-15 16:19:47 所属栏目:百科 来源:网络整理
导读:有没有人知道我可以在用户打开手机时检测到的方式?据我所知,android.intent.USER_PRESENT是在设备解锁时播放的(例如输入了正确的密码),但是,我不知道如何使用React native来检测广播.有没有人有办法解决吗? 解决方法 看看 AppState API in FB(Face Book) F
有没有人知道我可以在用户打开手机时检测到的方式?据我所知,android.intent.USER_PRESENT是在设备解锁时播放的(例如输入了正确的密码),但是,我不知道如何使用React native来检测广播.有没有人有办法解决吗?

解决方法

看看 AppState API in FB(Face Book)

FB上有3个状态,共有5个状态.我只在FB中看到3但其他2可能会或可能不会被支持.

活动 – 应用程序在前台运行

后台 – 该应用程序正在后台运行.用户在另一个应用程序或主屏幕上.

不活动 – 这是在前景和前景之间转换时发生的状态.背景,以及在不活动期间,例如进入多任务视图或来电时

查看苹果Docs了解更多相关信息.

当手机在锁屏中时,你将不得不测试什么状态.我不能告诉你什么状态使用,因为我从来没有测试过api.

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

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

我在这里以facebook为例,但在componentDidMount中附加更改事件列表器并在ComponentWillUnmount中删除,代码将运行到该状态.

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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读