Android工具类-关于网络、状态的工具类
来源:互联网 发布:linux修改文件用户权限 编辑:程序博客网 时间:2024/06/16 13:29
下方是一个很好的监测网络、状态的工具类
public class NetworkUtils {/** * 网络是否可用 * * @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) { * * @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;}}
以上方法均通过测试,tv_result为自设定的TextView。
tv_result.append("网络是否可用:"+NetworkUtils.isNetworkAvailable(MainActivity.this)+"\n");tv_result.append("GPS开关是否打开:"+NetworkUtils.isGpsEnabled(MainActivity.this)+"\n");tv_result.append("是否为3G网络:"+NetworkUtils.is3G(MainActivity.this)+"\n");tv_result.append("WIFI是否打开:"+NetworkUtils.isWifiEnabled(MainActivity.this)+"\n");tv_result.append("是否为WIFI网络:"+NetworkUtils.isWifi(MainActivity.this)+"\n");
- Android工具类-关于网络、状态的工具类
- Android 获取网络状态的工具类
- Android 获取网络状态的工具类
- Android判断网络状态的工具类
- 网络状态工具类
- android检查网络状态工具类
- Android:判断用户网络状态的Kotlin工具类
- 检测网络状态的工具类
- 判断网络状态的工具类__NetWorkUtil
- 工具类—得到网络状态的工具类NetStateUtil
- 判断网络状态工具类
- 网络状态判断工具类
- 判断网络状态工具类
- 判断网络状态工具类
- 网络状态判断工具类
- 判断网络状态工具类
- 网络状态判断工具类
- android 网络的工具类
- generate prompt
- iOS - 适配iphone5 及以上启动背景:
- chrome安全模式
- 使用Jmeter对应用程序进行测试
- php 获取时间今天明天昨天时间戳
- Android工具类-关于网络、状态的工具类
- 如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2 等
- C语言的数组名和对数组名取地址
- FTP端口20,21的区别
- chromium手势事件处理流程
- C语言实战应用第一课:获取系统当前时间
- 对hadoop版本YARN的一点理解
- 关于swap函数的使用注意事项
- 什么是BETA,RC,ALPHA版