Android -- network

[1] 在AndroidManifest 添加访问权限



[2] 获取当前手机的网络状态

package com.example.administrator.mydemoapp.client;

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

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.net.URL;

/**
 * Created by Administrator on 2017/9/2
 */

public class NetworkUtil {

    public static int NET_CNNT_BAIDU_OK = 1;
    public static int NET_CNNT_BAIDU_TIMEOUT = 2;
    public static int NET_NOT_PERPARE = 3;
    public static int NET_ERROR;
    private static int TIMEOUT=3000;

    /**
     * 检查网络
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context){
        ConnectivityManager manager = (ConnectivityManager)context.getApplicationContext().getSystemService(
                Context.CONNECTIVITY_SERVICE);
        if (null == manager)
            return false;
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (null == info || info.isAvailable())
            return false;
        return true;
    }

    /**
     * 获取IP地址
     * @return
     */
    public static String getLocalIpAddress(){
        String ret = "";
        try {
            for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
                NetworkInterface networkInterface=en.nextElement();
                for (Enumeration enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements();){
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()){
                        ret = inetAddress.getHostAddress().toString();
                    }
                }
            }
        }catch (SocketException ex){
            ex.printStackTrace();
        }
        return ret;
    }

    public static int getNetState(Context context){
        try {
            ConnectivityManager connectivity = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null){
                NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
                if (networkInfo != null){
                    if (networkInfo.isAvailable() && networkInfo.isConnected()){
                        if (! connectionNetwork())
                            return NET_CNNT_BAIDU_TIMEOUT;
                        else
                            return NET_CNNT_BAIDU_OK;
                    }else
                    {
                        return NET_NOT_PERPARE;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return NET_ERROR;
    }

    /**
     * ping "http://www.baidu.com"
     * @return
     */

    private static boolean connectionNetwork(){
        boolean result = false;
        HttpURLConnection httpUrl = null;
        try {
            httpUrl = (HttpURLConnection) new URL("http://www.baidu.com").openConnection();
            httpUrl.setConnectTimeout(TIMEOUT);
            httpUrl.connect();
            result = true;
        }catch (IOException e){

        }finally {
            if (null != httpUrl){
                httpUrl.disconnect();
            }
            httpUrl = null;
        }
        return result;
    }

    /**
     * 判断是否是3g网络
     * @param context
     * @return
     */
    public static boolean is3G(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE){
            return true;
        }
        return false;
    }

    /**
     * 判断是不是WIFI网络
     * @param context
     * @return
     */
    public static boolean isWifi(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI){
            return true;
        }
        return false;
    }

    /**
     * 判断是不是2G
     * @param context
     * @return
     */
    public static boolean is2G(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
                || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
                || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)){
            return true;
        }
        return false;
    }

    /**
     * wifi是否已连接上
     * @param context
     * @return
     */
    public static boolean isWifiEnabled(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return ((connectivityManager.getActiveNetworkInfo() != null
                && connectivityManager.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED
                || telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS));
    }

}

[3] 网络访问前调用

 if (NetworkUtil.isNetworkAvailable(context)){
//to do
}else{
//to do
}

[4] 需要对手机网络类别做判断的情形

(1) 下载视频,软件,文档等大文件。
(2) 缓存。
(3) 上传、发送文件给好友。

[5] 总结

大四了,看着新生入校,不知不觉我已走过了3年的青春。面对校招,不慌不乱,稳中求进。
代码学习了大神,感谢分享。

你可能感兴趣的:(Android -- network)