作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985,
转载请说明出处。
给大家分享一个Android开发者常用的工具类。主要针对网络判断的
功能强大。下面贴源代码。当然,在文章的最后,也有源代码的下载地址。
import java.util.List;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/*----------------------------------------------------------------
// 文件名:NetworkAvailableUtils
// 文件功能描述: 判断网络状态的工具类
//isNetworkAvailable 、isGpsEnabled、isWifiEnabled、is3rd、isWifi
//----------------------------------------------------------------*/
/**
* @author 博客:http://blog.csdn.net/qq_21376985
*
* David编写: 微博:http://weibo.com/93sec.cc
*
* @version V1.0正式版
*
* @process QQ986945193
*
* @Note weibo.com/mcxiaobing
*
* @dateTime 2015-10-18下午1:46:20
*
*/
public class NetworkAvailableUtils {
/**
* 判断是否联网
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
} else {
// 如果仅仅是用来判断网络连接
// 则可以使用 cm.getActiveNetworkInfo().isAvailable();
NetworkInfo[] info = cm.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
/**
* 判断GPS是否打开
*
* @param context
* @return
*/
public static boolean isGpsEnabled(Context context) {
LocationManager lm = ((LocationManager) context
.getSystemService(Context.LOCATION_SERVICE));
List<String> accessibleProviders = lm.getProviders(true);
return accessibleProviders != null && accessibleProviders.size() > 0;
}
/**
* 三、判断WIFI是否打开
* @param context
* @return
*/
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}
/**
* 四、判断是否是3G网络
* @param context
* @return
*/
public static boolean is3rd(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null
&& networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
/**
* 五、判断是wifi还是3g网络,用户的体现性在这里了,wifi就可以建议下载或者在线播放。
* @param context
* @return
*/
public static boolean isWifi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null
&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
}
源代码工具类免积分下载传送门:http://download.csdn.net/detail/qq_21376985/9564625