React-native Android -Network state change listener

in AndroidManifests file add network state change receiver:

<receiver android:name=".NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

In NetworkStateReceiver, once detect network state change, send broadcast

public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = manager.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnected() && netinfo.isAvailable()) {
Intent i = new Intent("RNNetInfoChange");
i.putExtra("isConnected", true);
context.sendBroadcast(i);
} else {
Intent i = new Intent("RNNetInfoChange");
i.putExtra("isConnected", false);
context.sendBroadcast(i);
}

}

In react-native module, register broadcast and send event to js

public class NetInfoModule extends ReactContextBaseJavaModule {
......
private void sendEvent( String eventName,
@Nullable WritableMap params) {
 mReactContext               .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
private void listenNetworkInfoChange() {
IntentFilter intentFilter = new IntentFilter("RNNetInfoChange");
this.context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Boolean v = intent.getBooleanExtra("isConnected",false);
WritableMap params = Arguments.createMap();
params.putBoolean(“networkinfo”,v);
sendEvent(“networkinfochange”, params);
}
},intentFilter);
}

in react-native, simply user DeviceEventEmitter to add listener

DeviceEventEmitter.addListener('networkinfochange', (data)=> {  
console.log('NetWorkInfoHasChanged!!!! isConnected:'+data);
});