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 中没有运气,下面是我的代码