I want to check if device is connected to internet or not. below is my code snippet:
public boolean isConnectedToInterNet() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
Above function returns false (even if connected to wifi/4g data) in background service and it returns true when app is in foreground.
activeNetworkInfo object returns below info:
NetworkInfo: type: WIFI[], state: DISCONNECTED/BLOCKED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false, simId: 0
Problem is specific to Redmi devices.
I enabled aouto-start also and isConnectedToInterNet() method returning true for all other devices.
I have seen all other comments below but problem is not solved