android判断是否连接网络

转自:http://www.cnblogs.com/mmll/articles/2043172.html

判断wifi网络是否链接

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

判断wifi是否连接(2):

   
   
   
   

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}




判断3G网络是否链接

 

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

    
    
    
    
 相关权限

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

你可能感兴趣的:(android,网络,service,null,NetWork)