'The device has 4G, but NetworkInfo.isConnected() show that there is no Internet connection
I have one strange reported by one of the clients. They are on 4G, but in the app it says that they do not have Internet connection.
In the app I am using this logic to inform the user that they have connection or not.
public class NetworkManager {
private final Context context;
private final SharedPreferences sharedPreferences;
@Inject public NetworkManager(@Named(DIConstants.APP) Context context, SharedPreferences sharedPreferences) {
this.context = context;
this.sharedPreferences = sharedPreferences;
}
public boolean isNetworkAvailable() {
if (context == null) {
return false;
}
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Network nw = connectivityManager.getActiveNetwork();
if (nw == null) { return false; }
try {
NetworkCapabilities network = connectivityManager.getNetworkCapabilities(nw);
return network != null && (network.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|| network.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|| network.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
} catch(SecurityException e) { // workaround for Android 11 bug
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
} else {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
public Flowable<Boolean> isOnlineModeAsync() {
return Flowable.just(isOnlineMode());
}
public boolean isOnlineMode() {
boolean isNetworkAvailable = isNetworkAvailable();
boolean isOfflineModeInSettings = sharedPreferences.getBoolean(Constants.SETTINGS_OFFLINE_MODE, Constants.OFFLINE_MODE_DEFAULT_VALUE);
if(isNetworkAvailable && !isOfflineModeInSettings) {
return true;
}
return false;
}
}
This is how it looks on the client device.
Any ideas?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

