android 网络连接判断

来源:互联网 发布:宏业软件最新版本 编辑:程序博客网 时间:2024/06/06 02:06

Android 网络判断类,用来判断网络状态

使用方法:

(1)先初始化

//初始化网络状态检测类
NetworkStateManager.instance().init(this);

(2)判断是否联网

 NetworkStateManager.instance().isNetworkConnected();

 

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
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
 
public class NetworkStateManager
{
    static NetworkStateManager s_m = null;
     
    private Context context;
     
    private NetworkStateManager()
    {
         
    }
     
    public void init(Context ctx)
    {
        context = ctx;
    }
     
    public static synchronized NetworkStateManager instance()
    {
        if (s_m == null)
        {
            s_m = new NetworkStateManager();
        }
        return s_m;
    }
     
    /**
     * 判断是否有网络连接
     * @return
     */
    public boolean isNetworkConnected()
    {
        if (context == null)
        {
            return false;
        }
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null)
        {
            return false;
        else
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
            {
                for (int i = 0; i < info.length; i++)
                {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    /**
     * 判断WIFI网络是否可用
     * @return
     */
    public boolean isWifiConnected()
    {
        if (context != null)
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mWiFiNetworkInfo != null)
            {
                return mWiFiNetworkInfo.isAvailable();
            }
        }
        return false;
    }
    /**
     * 判断MOBILE网络是否可用
     * @return
     */
    public boolean isMobileConnected()
    {
        if (context != null)
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mMobileNetworkInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mMobileNetworkInfo != null)
            {
                return mMobileNetworkInfo.isAvailable();
            }
        }
        return false;
    }
     
    public int getConnectedType()
    {
        if (context != null)
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null && mNetworkInfo.isAvailable())
            {
                return mNetworkInfo.getType();
            }
        }
        return -1;
    }
     
}

 参考 http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html

0 0