Android 判断网络连接_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android 判断网络连接

Android 判断网络连接

 2014/11/26 13:49:19  冰雪之淚  程序员俱乐部  我要评论(0)
  • 摘要:1importandroid.content.Context;2importandroid.net.ConnectivityManager;3importandroid.net.NetworkInfo;45/**6*网络工具类添加网络状态许可:<uses-permission7*android:name="android.permission.ACCESS_NETWORK_STATE"/>8*9*@authorbenny10*11*/12publicclassNetworkUtil
  • 标签: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 }

 

发表评论
用户名: 匿名