0

 state = {
    isConnected: null,
  };
  
    componentDidMount() {
    this._netWorkConnection();
  }
  
  componentWillUnmount() {
    this._netWorkConnection();
  }
  
    _netWorkConnection = () => {
    NetInfo.addEventListener(state => {
      console.log('Is connected?', state.isInternetReachable);

      const connection = state.isInternetReachable;

      this.setState(
        {
          isConnected: connection,
        },
        () => {
          if (this.state.isConnected === false) {
            this._showConnectionMessage();
          }
        },
      );
    });
  };

_showConnectionMessage() {
  Util.showMessage('No internet');
}
  
  

react-native-community / react-native-netinfo 监听器在 android 上不起作用,但它在 iOS 中运行良好,尽管我在我的 android 清单中添加了互联网权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> androidXCore = "1.0.2"在 build.gradle 中添加但在 android 中没有运气,下面是我的代码

4

1 回答 1

2

这段代码很好,只是不能在模拟器中工作,而在任何其他物理设备上都可以正常工作

于 2020-04-13T13:04:18.463 回答