network: Android 网络判断(wifi、3G与其他)
来源:互联网 发布:z-blog建站 编辑:程序博客网 时间:2024/05/16 11:45
package mark.zeng;
import java.util.List;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class NetworkProber {
/**
* 网络是否可用
*
* @param activity
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
} else {
NetworkInfo[] info = connectivity.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 locationManager = ((LocationManager) context
.getSystemService(Context.LOCATION_SERVICE));
List<String> accessibleProviders = locationManager.getProviders(true);
return accessibleProviders != null && accessibleProviders.size() > 0;
}
/**
* wifi是否打开
*/
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);
}
/**
* 判断当前网络是否是wifi网络
* if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { //判断3G网
*
* @param context
* @return boolean
*/
public static boolean isWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* 判断当前网络是否是3G网络
*
* @param context
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
}
另外还有两个方法判断网络是否可用:
public static boolean isNetworkAvailable_00(Context context) {
ConnectivityManager cm = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE));
if (cm != null) {
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isConnectedOrConnecting()) {
return true;
}
}
return false;
}
public static boolean isNetworkAvailable_01(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = cm.getActiveNetworkInfo();
if (network != null) {
return network.isAvailable();
}
return false;
}
更加严谨的写法:
public static boolean checkNet(Context context) {
try {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
return false;
}
return false;
}
- network: Android 网络判断(wifi、3G与其他)
- network: Android 网络判断(wifi、3G与其他)
- network: Android 网络判断(wifi、3G与其他)
- network: Android 网络判断(wifi、3G与其他)
- network: Android 网络判断(wifi、3G与其他)
- Android中判断有无可用网络(是否是3G或者WIFI网络)
- Android中判断有无可用网络(是否是3G或者WIFI网络)
- Android 网络连接--Wifi/3G
- Android 网络连接--Wifi/3G
- Android中判断有无可用网络的代码(是否是3G或者WIFI网络)
- Android下同时使用WIFI与3G网络(二)
- network: android 网络判断
- network: android 网络判断
- network: android 网络判断
- network: android 网络判断
- network: android 网络判断
- network: android 网络判断
- network: android 网络判断
- windbg常用命令
- OSI7层模型每层的作用--最彻底的解释
- 我的Android笔记(十)—— ProgressDialog的简单应用,等待提示
- 2004操作系统
- C语言中的 sizeof 问题
- network: Android 网络判断(wifi、3G与其他)
- JAVA 正则表达式 (超详细)
- jar包无法运行——could not find the main class...(java -jar *.jar可以运行,jar包不可以打开的解决方法)
- erlang lists 系列函数功能与用法详解(共68个函数)
- HDOJ-1796 DFS解容斥问题模板题..
- SOUND SERVICE系统音效服务+震动功能实现
- object-c学习:类别Category
- lcd fb参数如何计算
- Dell Fn键 F1-F12特殊功能键