天天看點

Android開發之強大的網絡判斷工具,判斷是否聯網,判斷是wifi還是3g網絡等java工具代碼類

作者:程式員小冰

轉載請說明出處。

import java.util.List;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/*----------------------------------------------------------------
 // 檔案名:NetworkAvailableUtils
 // 檔案功能描述: 判斷網絡狀态的工具類 
 //isNetworkAvailable 、isGpsEnabled、isWifiEnabled、is3rd、isWifi
 //----------------------------------------------------------------*/

/**  
 * @author 部落格
 *   
 *         David編寫: 微網誌:http://weibo.com/93sec.cc  
 *   
 * @version V1.0正式版 
 *   
 * @process 
 *   
 * @Note weibo.com/mcxiaobing  
 *   
 * @dateTime 2015-10-18下午1:46:20  
 *   
 */   
public class NetworkAvailableUtils {

    /**
     * 判斷是否聯網
     * 
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
        } else {
            //       如果僅僅是用來判斷網絡連接配接
            //       則可以使用 cm.getActiveNetworkInfo().isAvailable();
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判斷GPS是否打開
     * 
     * @param context
     * @return
     */

    public static boolean isGpsEnabled(Context context) {
        LocationManager lm = ((LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE));
        List<String> accessibleProviders = lm.getProviders(true);
        return accessibleProviders != null && accessibleProviders.size() > 0;
    }

    /**
     *  三、判斷WIFI是否打開
     * @param context
     * @return
     */

    public static boolean isWifiEnabled(Context context) {
        ConnectivityManager mgrConn = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager mgrTel = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
                .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
                .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
    }

    /**
     * 四、判斷是否是3G網絡
     * @param context
     * @return
     */

    public static boolean is3rd(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        if (networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }

    /**
     * 五、判斷是wifi還是3g網絡,使用者的展現性在這裡了,wifi就可以建議下載下傳或者線上播放。
     * @param context
     * @return
     */

    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkINfo = cm.getActiveNetworkInfo();
        if (networkINfo != null
                && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }
}