码迷,mamicode.com
首页 > 移动开发 > 详细

Android 判断网络连接

时间:2014-11-26 13:38:24      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   io   ar   color   sp   on   div   

 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 }

 

Android 判断网络连接

标签:android   style   blog   io   ar   color   sp   on   div   

原文地址:http://www.cnblogs.com/bennyyuan/p/4122825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!