React-native 监听App状态变化

  • active - 应用正在前台运行
  • background - 应用正在后台运行。用户可能面对以下几种情况:
    • 在别的应用中
    • 停留在桌面
    • 对 Android来说还可能处在另一个Activity中(即便是由你的应用拉起的)
  • inactive - 此状态表示应用正在前后台的切换过程中,或是处在系统的多任务视图,又或是处在来电状态中。
import {  AppState } from 'react-native';

componentWillMount() {
		// 监听App状态
        AppState.addEventListener('change', this._handleAppStateChange);
}

componentWillUnmount() {
        // 移除监听
        AppState.removeEventListener('change', this._handleAppStateChange);
}

// 监听app状态
_handleAppStateChange = (nextAppState) => {
     if (nextAppState === 'active') {
       	alert("APP应用正在前台运行")
     }
}
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐