react-native-community / react-native-netinfo列表器不适用于android

时间:2020-04-13 09:30:15

标签: android react-native react-native-android react-native-community-netinfo

 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" /> 中添加了Internet权限 并且在build.gradle中添加了androidXCore = "1.0.2",但是在android中却没有运气,下面是我的代码

1 个答案:

答案 0 :(得分:0)

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