android 检查联网状态

public class NetworkState {
    private static ConnectivityManager ctm;

    public NetworkState() {
    }

    public static boolean isOk(Activity act) {
        Context ctx = act.getApplicationContext();
        ctm = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo infos = ctm.getActiveNetworkInfo();
        if (infos != null && infos.isAvailable()) {
            return true;
        }
        return false;
    }

    public static String getNetType(Activity act) {
        if (isOk(act)) {
            String netType = ctm.getActiveNetworkInfo().getTypeName()
                    .toLowerCase();
            return netType;
        }
        return null;
    }
}
posted @ 2012-11-15 15:28  mr.coke  阅读(302)  评论(0编辑  收藏  举报