W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
AppStateIOS
可以告訴你應(yīng)用程序是在前臺還是在后臺,而且狀態(tài)更新時(shí)會通知你。 在處理推送通知時(shí),AppStateIOS 經(jīng)常被用于判斷目標(biāo)和適當(dāng)?shù)男袨椤?/p>
Active - 應(yīng)用程序在前臺運(yùn)行
Background - 應(yīng)用程序在后臺運(yùn)行。用戶正在使用另一個(gè)應(yīng)用程序或者在主屏幕上。
Inactive - 這是一種過渡狀態(tài),目前不會在React Native的應(yīng)用程序上發(fā)生。
想要獲取更多的信息,見 Apple's documentation
為了查看當(dāng)前的狀態(tài),你可以檢查 AppStateIOS.currentState
,該方法會一直保持最新狀態(tài)。然而,當(dāng) AppStateIOS
在橋接器上檢索currentState
時(shí),在啟動(dòng)時(shí)它將會為空。
getInitialState: function() { return { currentAppState: AppStateIOS.currentState, }; }, componentDidMount: function() { AppStateIOS.addEventListener('change', this._handleAppStateChange); }, componentWillUnmount: function() { AppStateIOS.removeEventListener('change', this._handleAppStateChange); }, _handleAppStateChange: function(currentAppState) { this.setState({ currentAppState, }); }, render: function() { return ( <Text>Current state is: {this.state.currentAppState}</Text> ); },
這個(gè)例子似乎只能說"當(dāng)前狀態(tài)是:活躍的"因?yàn)樵?nbsp;active
狀態(tài)時(shí),應(yīng)用程序只對用戶是可見的,空狀態(tài)只能是暫時(shí)的。
static addEventListener(type: string, handler: Function)
通過監(jiān)聽 change
事件類型和提供處理程序,為應(yīng)用程序狀態(tài)變化添加一個(gè)處理程序。
static removeEventListener(type: string, handler: Function)
通過傳遞 change
事件類型和處理程序,刪除一個(gè)處理程序。
'use strict'; var React = require('react-native'); var { AppStateIOS, Text, View } = React; var AppStateSubscription = React.createClass({ getInitialState() { return { appState: AppStateIOS.currentState, previousAppStates: [], }; }, componentDidMount: function() { AppStateIOS.addEventListener('change', this._handleAppStateChange); }, componentWillUnmount: function() { AppStateIOS.removeEventListener('change', this._handleAppStateChange); }, _handleAppStateChange: function(appState) { var previousAppStates = this.state.previousAppStates.slice(); previousAppStates.push(this.state.appState); this.setState({ appState, previousAppStates, }); }, render() { if (this.props.showCurrentOnly) { return ( <View> <Text>{this.state.appState}</Text> </View> ); } return ( <View> <Text>{JSON.stringify(this.state.previousAppStates)}</Text> </View> ); } }); exports.title = 'AppStateIOS'; exports.description = 'iOS app background status'; exports.examples = [ { title: 'AppStateIOS.currentState', description: 'Can be null on app initialization', render() { return <Text>{AppStateIOS.currentState}</Text>; } }, { title: 'Subscribed AppStateIOS:', description: 'This changes according to the current state, so you can only ever see it rendered as "active"', render(): ReactElement { return <AppStateSubscription showCurrentOnly={true} />; } }, { title: 'Previous states:', render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; } }, ];
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: