Back to project page CommonLibs.
The source code is released under:
Apache License
If you think the Android project CommonLibs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.alex.common.utils; // www . j a v a2s . com import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; /** * ??????? * @author caisenchuan */ public class NetworkUtils { /*-------------------------- * ??? *-------------------------*/ /** * ?????? */ public enum NetworkType { /**????*/ NETWORK_UNKNOWN, /**???*/ NETWORK_NONE, /**Wifi??WiMax*/ NETWORK_WIFI, /**????*/ NETWORK_MOBILE, /**???*/ NETWORK_ETHERNET, /**????*/ NETWORK_OTHERS } /*-------------------------- * ????? *-------------------------*/ /*-------------------------- * ???????? *-------------------------*/ /*-------------------------- * public?? *-------------------------*/ /** * ??????????? */ public static NetworkType getCurrentNetworkType(Context context) { NetworkType ret = NetworkType.NETWORK_UNKNOWN; if (context != null) { ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if(manager != null) { NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null) { boolean available = info.isAvailable(); if(available) { int type = info.getType(); switch(type) { case ConnectivityManager.TYPE_WIFI: case ConnectivityManager.TYPE_WIMAX: ret = NetworkType.NETWORK_WIFI; break; case ConnectivityManager.TYPE_MOBILE: case ConnectivityManager.TYPE_MOBILE_DUN: case ConnectivityManager.TYPE_MOBILE_HIPRI: case ConnectivityManager.TYPE_MOBILE_MMS: case ConnectivityManager.TYPE_MOBILE_SUPL: ret = NetworkType.NETWORK_MOBILE; break; case ConnectivityManager.TYPE_ETHERNET: ret = NetworkType.NETWORK_ETHERNET; break; default: ret = NetworkType.NETWORK_OTHERS; break; } } else { ret = NetworkType.NETWORK_NONE; } } } } return ret; } /*-------------------------- * protected??packet?? *-------------------------*/ /*-------------------------- * private?? *-------------------------*/ }