码迷,mamicode.com
首页 > Web开发 > 详细

NetWorkUtils

时间:2015-12-18 14:41:03      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

/**
 * 
 */
public class NetWorkUtils {
    /**
     * 获取当前网络类型
     *
     * @param context
     * @return -1 没有网络
     * 0 移动网络;
     * 1 wifi;
     * 2 其他;
     * @throws Exception
     */
    public static int getNetWorkType(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork == null)
            return -1;
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            return 1;
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            return 0;
        } else {
            return 2;
        }
    }

    public static boolean isMobileType(Context context) {
        return 0 == getNetWorkType(context);
    }

    public static boolean isConnected(Context context) {
        return -1 != getNetWorkType(context);
    }

    public static void openWifi(Activity context) {
         context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    }
}

 

NetWorkUtils

标签:

原文地址:http://www.cnblogs.com/yangcong/p/5056924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!