So in this app, it is required to use some sort of network because there's API calls involved. There's an error that pops up when there's no mobile data around. The thing is, it also pops up when I turn the WiFi off, while the mobile data is still on. Is there a way to only have the error message pop up for the mobile data status only and not both the WiFi and mobile data. Here's the code I believe is verifying the network state.
public static boolean verifyNetWork(@NonNull Context context) {
ConnectivityManager cm = (ConnectivityManager)context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return false;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
Any help and advice is appreciated. Thanks
Note that you need to add the
ACCESS_NETWORK_STATEpermission to your app'sAndroidManifest.xmlfile in order to use theConnectivityManagerclass:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />To check for the availability of mobile data in an Android app, you can use the
ConnectivityManagerclass provided by the Android framework.You can then call this method in your code and check its return value to determine whether mobile data is available:
Yes, the updated code I provided checks for both mobile data and Wi-Fi.