网络判断utils
来源:互联网 发布:复合材料刚度矩阵 编辑:程序博客网 时间:2024/06/03 16:47
首先 清单文件加权限:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>public class NetWorkHelper {/** * 判断是否有网络连接 */public static boolean isNetworkAvailable(Context context) {ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity == null) {Toast.makeText(context, "couldn't get connectivity manager", 1).show();} else {NetworkInfo[] info = connectivity.getAllNetworkInfo();if (info != null) {for (int i = 0; i < info.length; i++) {if (info[i].isAvailable()) {Toast.makeText(context, "network is available", 1).show();return true;}}}}Toast.makeText(context, "network is not available", 1).show();return false;}/** * 判断网络是否为漫游 */public static boolean isNetworkRoaming(Context context) {ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity == null) {Toast.makeText(context, "couldn't get connectivity manager", 1).show();NetworkInfo info = connectivity.getActiveNetworkInfo();if (info != null&& info.getType() == ConnectivityManager.TYPE_MOBILE) {TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);if (tm != null && tm.isNetworkRoaming()) {Toast.makeText(context, "network is roaming", 1).show();return true;} else {Toast.makeText(context, "network is not roaming", 1).show();}} else {Toast.makeText(context, "not using mobile network", 1).show();}}return false;}/** * 判断MOBILE网络是否可用 * * @param context * @return * @throws Exception */public static boolean isMobileDataEnable(Context context) throws Exception {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);boolean isMobileDataEnable = false;isMobileDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();Toast.makeText(context, "MOBILE网络 is "+isMobileDataEnable, 1).show();return isMobileDataEnable;}/** * 判断wifi 是否可用 * @param context * @return * @throws Exception */public static boolean isWifiDataEnable(Context context) throws Exception {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);boolean isWifiDataEnable = false;isWifiDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();Toast.makeText(context, "WIFI is "+isWifiDataEnable, 1).show();return isWifiDataEnable;}/** * 判断网络类型 * @param context * @return * @throws Exception */public static int getConnectedType(Context context) throws Exception {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();if(networkInfo!=null&&networkInfo.isAvailable()){Toast.makeText(context, "type is "+networkInfo.getType(), 1).show();return networkInfo.getType();}return -1;}}
0 0
- 网络判断utils
- Utils联网判断
- UTILS
- Utils
- utils
- utils
- Utils
- Utils
- Utils
- utils
- Utils
- Utils
- Utils
- UTILS
- Utils
- Utils
- Utils
- Utils
- WIN7无法打开CHM解决办法
- 思维导图:大脑使用说明书----读后
- Linux系统编程---线程同步---互斥量
- HighCharts之2D柱状图、折线图的组合多轴图
- 呕心沥血,本人写的单片机例程-----多模式随机显示流水灯_流水灯高级篇
- 网络判断utils
- 第一天
- Linux系统编程---线程同步---条件变量
- 覆盖的面积(线段树-扫描线)
- 黑马程序员 Java基础<五>异常
- 一般树形结构_求通路长度
- 调试代码
- C++收获
- VS2010 + VTK+QT的安装教程