Android 判断网络连接

 1 import android.content.Context;
 2 import android.net.ConnectivityManager;
 3 import android.net.NetworkInfo;
 4 
 5 /**
 6  * 网络工具类 添加网络状态许可:<uses-permission
 7  * android:name="android.permission.ACCESS_NETWORK_STATE"/>
 8  * 
 9  * @author benny
10  * 
11  */
12 public class NetworkUtil {
13 
14     /**
15      * 网络连接状态
16      *  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
17      * @param context
18      *            程序上下文
19      * @return true:可连接
20      */
21     public static boolean isNetworkConnected(Context context) {
22         if (context != null) {
23             ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
24 
25             NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
26             if (networkInfo != null) {
27                 return networkInfo.isAvailable();
28             }
29         }
30         return false;
31     }
32 
33     /**
34      * WiFi 网络连接状态
35      *  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
36      * @param context
37      *            程序上下文
38      * @return true:可连接
39      */
40     public static boolean isWifiConnected(Context context) {
41         if (context != null) {
42             ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
43 
44             NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
45             if (networkInfo != null) {
46                 return networkInfo.isAvailable();
47             }
48         }
49         return false;
50     }
51 
52     /**
53      * Mobile 网络连接状态
54      * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
55      * 
56      * @param context
57      *            程序上下文
58      * @return true:可连接
59      */
60     public static boolean isMobileConnected(Context context) {
61         if (context != null) {
62             ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
63 
64             NetworkInfo networkInfo =  connectivityManager.getActiveNetworkInfo();
65             if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
66                     return networkInfo.isAvailable();
67             }
68         }
69         return false;
70     }
71 
72     /**
73      * 获取网络连接类型
74      * @param context 程序上下文
75      * @return ConnectivityManager.TYPE_XXXX
76      */
77     public static int getConnectedType(Context context) {
78         if (context != null) {
79             ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
80             NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();
81             if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
82                 return mNetworkInfo.getType();
83             }
84         }
85         return -1;
86     }
87 
88 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。