1 package com.way.util; 2 3 import android.content.Context; 4 import android.net.ConnectivityManager; 5 import android.net.NetworkInfo.State; 6 7 public class NetUtil { 8 public static final int NETWORN_NONE = 0; 9 public static final int NETWORN_WIFI = 1;10 public static final int NETWORN_MOBILE = 2;11 12 public static int getNetworkState(Context context) {13 ConnectivityManager connManager = (ConnectivityManager) context14 .getSystemService(Context.CONNECTIVITY_SERVICE);15 16 // Wifi17 State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)18 .getState();19 if (state == State.CONNECTED || state == State.CONNECTING) {20 return NETWORN_WIFI;21 }22 23 // 3G24 state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)25 .getState();26 if (state == State.CONNECTED || state == State.CONNECTING) {27 return NETWORN_MOBILE;28 }29 return NETWORN_NONE;30 }31 }