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

android 判断网络连接的工具类

时间:2015-10-15 22:01:59      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

  

 1 package com.way.util;
 2 
 3 import android.content.Context;
 4 import android.net.ConnectivityManager;
 5 import android.net.NetworkInfo.State;
 6 
 7 public class NetUtil {
 8     public static final int NETWORN_NONE = 0;
 9     public static final int NETWORN_WIFI = 1;
10     public static final int NETWORN_MOBILE = 2;
11 
12     public static int getNetworkState(Context context) {
13         ConnectivityManager connManager = (ConnectivityManager) context
14                 .getSystemService(Context.CONNECTIVITY_SERVICE);
15 
16         // Wifi
17         State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
18                 .getState();
19         if (state == State.CONNECTED || state == State.CONNECTING) {
20             return NETWORN_WIFI;
21         }
22 
23         // 3G
24         state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
25                 .getState();
26         if (state == State.CONNECTED || state == State.CONNECTING) {
27             return NETWORN_MOBILE;
28         }
29         return NETWORN_NONE;
30     }
31 }

 

android 判断网络连接的工具类

标签:

原文地址:http://www.cnblogs.com/Sunnor/p/4883609.html

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