Android Open Source - CommonLibs Network Utils






From Project

Back to project page CommonLibs.

License

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.

Java Source Code

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??
     *-------------------------*/

}




Java Source Code List

com.alex.common.AppConfig.java
com.alex.common.AppControl.java
com.alex.common.Err.java
com.alex.common.OnHttpRequestReturnListener.java
com.alex.common.activities.BaseActivity.java
com.alex.common.activities.ImageLoadActivity.java
com.alex.common.activities.WebViewActivity.java
com.alex.common.apis.HttpApi.java
com.alex.common.exception.RetErrorException.java
com.alex.common.utils.BackgroundHandler.java
com.alex.common.utils.BaiduMapUtils.java
com.alex.common.utils.DateTimeUtils.java
com.alex.common.utils.DeviceUtils.java
com.alex.common.utils.DialogUtils.java
com.alex.common.utils.FileUtils.java
com.alex.common.utils.ImageUtils.java
com.alex.common.utils.KLog.java
com.alex.common.utils.Misc.java
com.alex.common.utils.NetworkUtils.java
com.alex.common.utils.PrefUtils.java
com.alex.common.utils.ShareUtils.java
com.alex.common.utils.StringUtils.java
com.alex.common.utils.ThreadUtils.java
com.alex.common.utils.ToastUtils.java
com.alex.common.views.ZoomImageView.java