Check if your mobile phone supports WIFI or Data / 3G

Assuming both WIFI and Data / 3G are installed on the device, how to check if the user is currently using the Internet via Wi-Fi or a data plan, assuming that they are both turned on. Therefore, I do not need to check whether they are enabled or disabled, I would like to determine which user is using it.

I did the following: is this the only solution?

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) { String ssid = wifiInfo.getSSID(); } 
+8
android
source share
4 answers
 void chkStatus() { final ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (wifi.isConnectedOrConnecting ()) { Toast.makeText(this, "Wifi", Toast.LENGTH_LONG).show(); } else if (mobile.isConnectedOrConnecting ()) { Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show(); } } 
+19
source share

You can use the following method to check network status each time:

 public static String checkNetworkStatus(final Context context) { String networkStatus = ""; // Get connect mangaer final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // check for wifi final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // check for mobile data final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if( wifi.isAvailable() ) { networkStatus = "wifi"; } else if( mobile.isAvailable() ) { networkStatus = "mobileData"; } else { networkStatus = "noNetwork"; } return networkStatus; } // end checkNetworkStatus 

// vKj

+5
source share

getNetworkInfo is an obsolete method. I suggest using getActiveNetworkInfo

Example

 private String checkNetworkStatus(Context context) { String networkStatus =""; final ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); //Check Wifi final android.net.NetworkInfo wifi = manager.getActiveNetworkInfo(); //Check for mobile data final android.net.NetworkInfo mobile = manager.getActiveNetworkInfo(); if( wifi.getType() == ConnectivityManager.TYPE_WIFI) { networkStatus = "wifi"; }else if(mobile.getType() == ConnectivityManager.TYPE_MOBILE){ networkStatus = "mobileData"; }else{ networkStatus="noNetwork"; } return networkStatus; } 
+1
source share
  public boolean CheckConnectivity(final Context c) { ConnectivityManager mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); if (mConnectivityManager.getActiveNetworkInfo() != null && mConnectivityManager.getActiveNetworkInfo().isAvailable() && mConnectivityManager.getActiveNetworkInfo().isConnected()) { return true; } else { return false; // make it false } } 
0
source share

All Articles