hxb
2022-11-22 b3513b1713bb979d0a69c5a8c4ddcd038f184e6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.mm.android.deviceaddmodule.mobilecommon.utils;
 
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
 
public class NetWorkHelper {
    
/**
     * 获取当前网络类型
     * @param context
     * @return
     */
    public static int checkNetwork(Context context) {
        int flag = -1;
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conMan == null) {
            flag = -1;
        } else {
            NetworkInfo[] info = conMan.getAllNetworkInfo();
            if (info != null) {
                for (NetworkInfo anInfo : info) {
                    if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                        flag = anInfo.getType();
                        break;
                    }
                }
            }
        }
        return flag;
    }
    
    /**
     * 获取当前网络是否连接
     * @param context
     * @return
     */
    public static boolean isConnected(Context context) {
        if(context == null){
            return false;
        }
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(conn == null){
            return false;
        }
        NetworkInfo info = conn.getActiveNetworkInfo();
        return (info != null && info.isAvailable());
    }
    
 
    public static boolean isWifiNetworkAvailable(Context context) {
        if(context == null)return false;
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            // 判断wifi
            return true;
        }
        return false;
    }
 
 
 
 
 
 
    public static int getIpV4Value(String ipOrMask) {
        byte[] addr = getIpV4Bytes(ipOrMask);
        int address = addr[3] & 0xFF;
        address |= ((addr[2] << 8) & 0xFF00);
        address |= ((addr[1] << 16) & 0xFF0000);
        address |= ((addr[0] << 24) & 0xFF000000);
        return address;
    }
 
    public static byte[] getIpV4Bytes(String ipOrMask) {
        try{
            String[] addrs = ipOrMask.split("\\.");
            int length = addrs.length;
            byte[] addr = new byte[length];
            for(int i = 0; i < length; i++) {
                addr[i] = (byte)(Integer.parseInt(addrs[i]) & 0xff);
            }
            return addr;
        }catch (Exception e) {
 
        }
        return new byte[4];
    }
 
 
    
    /**
     * 网络是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        android.net.ConnectivityManager connectivity = (android.net.ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE); // 获取系统网络连接管理�?
        if (connectivity == null) { // 如果网络管理器为null
            return false; // 返回false表明网络无法连接
        } else {
            android.net.NetworkInfo[] info = connectivity.getAllNetworkInfo(); // 获取�?��的网络连接对�?
            if (info != null) { // 网络信息不为null
                for (NetworkInfo anInfo : info) { // 遍历网路连接对象
                    if (anInfo.isConnected()) { // 当有�?��网络连接对象连接上网络时
                        return true; // 返回true表明网络连接正常
                    }
                }
            }
        }
        return false;
    }
    
 
 
}