关闭

一些网络判断方法

标签: 网络
71人阅读 评论(0) 收藏 举报
分类:
public class NetworkUtils {

    /**
     * @Title: checkingNetStatus
     * @Description: 判断网络状态
     * @param: @param context
     * @param: @return
     * @return: boolean false:网络异常;true 网络正常
     */
    public static boolean checkingNetStatus(Context context) {
        boolean result = false;
        ConnectivityManager conManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networInfo = conManager.getActiveNetworkInfo(); // 取得网络连接相关信息
        if (networInfo == null || !networInfo.isAvailable()) // 网络链接异常
        {
            result = false;
        } else {
            result = true;
        }
        return result;
    }

    /**
     * 打开网络设置
     */
    public static void openNetSet(Context context) {
        if (android.os.Build.VERSION.SDK_INT > 10) {
            // 3.0以上打开设置界面,也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
            context.startActivity(new Intent(
                    android.provider.Settings.ACTION_SETTINGS));
        } else {
            context.startActivity(new Intent(
                    android.provider.Settings.ACTION_WIRELESS_SETTINGS));
        }
    }

    /**
     * 检测手机是否开启GPRS网络,需要调用ConnectivityManager,TelephonyManager 服务.
     * 
     * @param context
     * @return boolean
     */
    public static boolean checkGprsNetwork(Context context) {
        boolean has = false;
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager mTelephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        int netType = info.getType();
        int netSubtype = info.getSubtype();
        if (netType == ConnectivityManager.TYPE_MOBILE
                && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
                && !mTelephony.isNetworkRoaming()) {
            has = info.isConnected();
        }
        return has;

    }
    public static int checkNetType(Context context){
        int type=0;
        //0--网络未连接
        //1-wifi
        //2-流量
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo.State gprs = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        NetworkInfo.State wifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if(gprs == NetworkInfo.State.CONNECTED || gprs == NetworkInfo.State.CONNECTING){
                type=2;
        }
        //判断为wifi状态下才加载广告,如果是GPRS手机网络则不加载!
        else if(wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING){
                type=1;
        }else {
            type=0;
        }
        return type;
    }


    /**
     * 检测手机是否开启WIFI网络,需要调用ConnectivityManager服务.
     *
     * @param context
     * @return boolean
     */
    public static boolean checkWifiNetwork(Context context) {
        boolean has = false;
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        int netType = info.getType();
        int netSubtype = info.getSubtype();
        if (netType == ConnectivityManager.TYPE_WIFI) {
            has = info.isConnected();
        }
        return has;
    }

    /**
     * 检测当前手机是否联网
     *
     * @param context
     * @return boolean
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 手机是否处在漫游
     *
     * @param mCm
     * @return boolean
     */
    public boolean isNetworkRoaming(Context mCm) {
        ConnectivityManager connectivity = (ConnectivityManager) mCm
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        }
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
        TelephonyManager mTm = (TelephonyManager) mCm
                .getSystemService(Context.TELEPHONY_SERVICE);
        boolean isRoaming = isMobile && mTm.isNetworkRoaming();
        return isRoaming;
    }


    public static String getImeiId(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        return imei;
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13329次
    • 积分:502
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类