【发布时间】: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