SoFunction
Updated on 2025-03-11

How to determine whether to connect to the network by programming Android [WiFi and 3G judgment]

This article describes the method of Android programming to determine whether to connect to the network. Share it for your reference, as follows:

Determine whether the wifi network is linked:

public static boolean isWiFiActive(Context inContext) {
     WifiManager mWifiManager = (WifiManager) inContext
     .getSystemService(Context.WIFI_SERVICE);
     WifiInfo wifiInfo = ();
     int ipAddress = wifiInfo == null ? 0 : ();
     if (() && ipAddress != 0) {
     ("**** WIFI is on");
       return true;
     } else {
       ("**** WIFI is off");
       return false;
     }
}

Determine whether the 3G network is linked:

public static boolean isNetworkAvailable( Context context) {
  ConnectivityManager connectivity = (ConnectivityManager) (Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
      ("**** newwork is off");
      return false;
    } else {
      NetworkInfo info = ();
      if(info == null){
        ("**** newwork is off");
        return false;
      }else{
        if(()){
          ("**** newwork is on");
          return true;
        }
      }
    }
     ("**** newwork is off");
  return false;
}

Related permissions:

<uses-permission android:name=".ACCESS_NETWORK_STATE" />
<uses-permission android:name=".ACCESS_WIFI_STATE" />

PS: For file-related attribute functions, please refer to the online tools of this site:

Android Manifest function and permission description:
http://tools./table/AndroidManifest

For more information about Android related content, please check out the topic of this site:Android communication methods summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.