ReactNative 退出到后台一定时间之后,跳转到登录界面

来源:互联网 发布:德国pjur怎么样 知乎 编辑:程序博客网 时间:2024/06/06 16:37
****实现长时间不操作应用,可以自定义多长时间,直接跳转到登录界面,登录之后才能再进行操作****let pTimes;let BACK_TO_LOGIN_TIME = 120;constructor(props) {   super(props);   this.state={       currentAppState:AppState.currentState,   }}componentDidMount() {    AppState.addEventListener('change', this.handleAppStateChange);    pTimes =-1;}componentWillUnmount() {    AppState.removeEventListener('change', this.handleAppStateChange);}handleAppStateChange = (nextAppState) => {    if (this.state.currentAppState.match(/inactive|background/) && nextAppState === 'active') {        let tempTime = getCurrentTime();        let waste = tempTime -pTimes;        console.log('----'+preTimes+'----waste:' + waste)        console.log('BACK_TO_LOGIN_TIME====='+BACK_TO_LOGIN_TIME)        if (waste > BACK_TO_LOGIN_TIME) {            InteractionManager.runAfterInteractions(()=>{                this.props.navigator.resetTo({                    component:Login,                    name:'Login',                })            })        toastShort('长时间未操作,需要重新登录')        console.log('AppState:'+'It's too late,login again')        }else{            console.log('AppState:'+'回来的很及时')        }        console.log('AppState:'+'现在在前台')    }else{        //前台切换至后台        console.log('AppState:'+'现在在后台')        preTimes = getCurrentTime();    }    this.setState({ currentAppState:nextAppState});    console.log('pTimes:'+pTimes);}//返回从1970年1月1日至今的毫秒数export function getCurrentTime(){    let date = new Date();    return date.getTime();}
阅读全文
0 0
原创粉丝点击