RN-AppState

ReactNative 提供了AppState 来告知 App当前状态:前台运行中、后台台运行中

1 获取当前应用状态

AppState.currentState

* active 前台运行中
* background 后台运行中
* inactive  运行的过渡状态

2 代码示例

/**
 * Created by licc on 2017/7/9.
 */

import React, {Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    AppState
} from 'react-native';

import NavigationBar from './NavigationBar'


export default class AppStateExample extends Component {

    componentWillMount(){

        //监听状态改变
        AppState.addEventListener('change', this.changeState);

        //监听内存报警
        AppState.addEventListener('memoryWarning', ()=>{console.log('内存报警....')});
    }

    componentWillUnmount(){
        AppState.removeEventListener('change', this.changeState());
    }

    changeState(state){
        alert('当前状态:'+state);
    }

    render() {
        return (
            <View style={styles.container}>
                <NavigationBar
                    title={'AlertIOS'}
                    statusBar={{backgroundColor:'blue'}}
                />
                <Text style={styles.item}>监听中...</Text>
            </View>
        );
    }
}

const styles = StyleSheet.create({

    container:{
        flex:1
    },

    item:{
        marginTop:10,
        marginLeft:5,
        marginRight:5,
        height:30,
        borderWidth:1,
        padding:6,
        borderColor:'#ddd',
        textAlign:'center'
    },
});

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,822评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 自从古老的iOS4以来,当用户点击home建的时候,你可以使你的APP们在内存中处于suspended(挂起)状态...
    木易林1阅读 3,245评论 1 4
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,305评论 4 61
  • 人与兽 孰对孰错? 1 浙江宁波雅戈尔动物园发生老虎咬人事件。截止到今天为止,被咬的人已经死亡,咬人的老虎也已经被...
    丑妹电影阅读 314评论 0 0