【问题标题】:react-native-community / react-native-netinfo listners are not working on androidreact-native-community / react-native-netinfo 监听器不在android上工作
【发布时间】:2020-07-25 20:05:30
【问题描述】:

 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 manifest <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 中添加了互联网权限 并在 build.gradle 中添加了 androidXCore = "1.0.2" 但在 android 中没有运气,下面是我的代码

【问题讨论】:

  • 请说明您使用的是哪个版本的 React Native,以及您使用的是哪个安卓操作系统。
  • 反应原生版本 0.61.5 所有安卓设备@NeetinSolanki
  • 你是否从 android/build.gradle 中删除了 'supportLibVersion' 属性?
  • 这里我已经尝试过 supportLibVersion 和 androidXCore 但它不起作用@NeetinSolanki ext { buildToolsVersion = "28.0.3" minSdkVersion = 16 compileSdkVersion = 28 targetSdkVersion = 28 // androidXCore = "1.0 .2" supportLibVersion = "28.0.0" }
  • 一次只能添加一个。添加 supportLibVersion 或 androidXCore,不能同时添加。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2019-10-14
    • 2022-01-17
    • 2020-05-10
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多