How to tell if 'Mobile Network Data' is enabled or disabled (even when connected by WiFi)?
The following code will tell you if "mobile data" is enabled or not, regardless of whether or not there is a mobile data connection active at the moment or whether or not wifi is enabled/active or not. This code only works on Android 2.3 (Gingerbread) and later. Actually this code also works on earlier versions of Android as well ;-)
boolean mobileDataEnabled = false; // Assume disabled
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true); // Make the method callable
// get the setting for "mobile data"
mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
// Some problem accessible private API
// TODO do whatever error handling you want here
}
Note: you will need to have permission android.permission.ACCESS_NETWORK_STATE
to be able to use this code.
Checking if internet connection over WiFi or mobile data available
First ask for this permission to the manifest.xml : android.permission.ACCESS_NETWORK_STATE
In android, we can determine the internet connection
status easily by using WIFIMANAGER.
private boolean LookForWifiOnAndConnected() {
WifiManager wifi_m = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
if (wifi_m.isWifiEnabled()) { // if user opened wifi
WifiInfo wifi_i = wifi_m.getConnectionInfo();
if( wifi_i.getNetworkId() == -1 ){
return false; // Not connected to any wifi device
}
return true; // Connected to some wifi device
}
else {
return false; // user turned off wifi
}
}
// check if mobile data on or off
boolean mobileDataEnabled = false; // Assume disabled
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true); // Make the method callable
// get the setting for "mobile data"
mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
// Some problem accessible private API
// TODO do whatever error handling you want here
}
How to check if both Mobile Data and WIFI are connected?
I never found a way to make the networkInfoWifi.isConnected()
andnetworkInfoMobileData.isConnected()
to return both TRUE. This is probably because Android can only be connected to only one interface at a time.
This interface selection is also done by Android automatically starting Lollipop devices. Android Lollipop defaults to Mobile Data when Wi-Fi has not Internet access?
How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?
Here is code snippet. It returns true if network enable ,else false
private boolean netCheckin() {
try {
ConnectivityManager nInfo = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
Log.d(tag, "Net avail:"
+ nInfo.getActiveNetworkInfo().isConnectedOrConnecting());
ConnectivityManager cm = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Log.d(tag, "Network available:true");
return true;
} else {
Log.d(tag, "Network available:false");
return false;
}
} catch (Exception e) {
return false;
}
}
Related Topics
How to Get All Android Contacts But Without Those Which Are on Sim
How to Set Internet Options for Android Emulator
Asynctask and Looper.Prepare() Error
Android - Set Text to Textview
Scan_Results_Available_Action Return Empty List in Android 6.0
How to Integrate Opencv Manager in Android App
How to Programmatically Add Buttons into Layout One by One in Several Lines
Execution Failed for Task 'App:Mergedebugresources' Crunching Cruncher....Png Failed
Trigger Mediascanner on Specific Path (Folder), How To
Bluetooth Low Energy Startscan on Android 6.0 Does Not Find Devices
Passing String Array Between Android Activities
Android: How to Pass Parcelable Object to Intent and Use Getparcelable Method of Bundle
Alert Dialog from Android Service
Noclassdeffounderror When Googleanalyticstracker.Getinstance()
Java.Lang.Noclassdeffounderror: Javax.Activation.Datahandler in Android
How to Use the Firebase Server Timestamp to Generate Date Created