Android Open Source - android-util Net Util






From Project

Back to project page android-util.

License

The source code is released under:

Apache License

If you think the Android project android-util 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.ms.square.android.util;
/*from  ww  w .  j  a  v a  2s . co  m*/
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

import java.util.Map;

public class NetUtil {

    public static final String buildUrl(String baseUrl, Map<String, String> params) {
        Uri.Builder uri = new Uri.Builder();
        uri.path(baseUrl);
        for(String key : params.keySet()) {
            uri.appendQueryParameter(key, params.get(key));
        }
        return Uri.decode(uri.build().toString());
    }

    public static boolean isNetworkConnected(Context context) {
        NetworkInfo activeNetwork = getActiveNetwork(context);
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnected();

        return isConnected;
    }

    public static boolean isWifiConnected(Context context) {
        NetworkInfo activeNetwork = getActiveNetwork(context);
        boolean isWifiConnected = activeNetwork != null && activeNetwork.isConnected() &&
                activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

        return isWifiConnected;
    }

    public static boolean isWifiOrWiMaxConnected(Context context) {
        NetworkInfo activeNetwork = getActiveNetwork(context);
        boolean isWifiWiMaxConnected = activeNetwork != null && activeNetwork.isConnected() &&
                (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_WIMAX);

        return isWifiWiMaxConnected;
    }

    private static NetworkInfo getActiveNetwork(Context context) {
        ConnectivityManager cm =
                (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

        return cm.getActiveNetworkInfo();
    }
}




Java Source Code List

com.ms.square.android.util.AnimUtil.java
com.ms.square.android.util.AppUtil.java
com.ms.square.android.util.CameraUtil.java
com.ms.square.android.util.FileUtil.java
com.ms.square.android.util.LogUtil.java
com.ms.square.android.util.NetUtil.java
com.ms.square.android.util.StreamUtil.java
com.ms.square.android.util.StringUtil.java
com.ms.square.android.util.ToastMaster.java
com.ms.square.android.util.UIUtil.java