zoukankan      html  css  js  c++  java
  • NetUtils网络连接工具类

    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    public class NetUtils {
    
       /**
        * 获得网络连接是否可用
        * @param context
        * @return
        */
       public static boolean hasNetwork(Context context) {
          ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo workinfo = con.getActiveNetworkInfo();
          if (workinfo == null || !workinfo.isAvailable()) {
             return false;
          }
          return true;
       }
    
       /**
        * 判断是否是wifi连接
        */
       public static boolean checkWifiState(Context context) {
          boolean isWifiConnect = true;
          ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
          for (int i = 0; i < networkInfos.length; i++) {
             if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
                if (networkInfos[i].getType() == cm.TYPE_MOBILE) {
                   isWifiConnect = false;
                }
                if (networkInfos[i].getType() == cm.TYPE_WIFI) {
                   isWifiConnect = true;
                }
             }
          }
          return isWifiConnect;
       }
    
       /**
        * 打开网络设置界面
        */
       public static void openNet(Activity activity) {
          Intent intent = new Intent("/");
          ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
          intent.setComponent(cm);
          intent.setAction("android.intent.action.VIEW");
          activity.startActivityForResult(intent, 0);
       }
    }
    
  • 相关阅读:
    VSCode settings.json的配置样例
    用css让div高度自动撑满屏幕
    C# 后端接受前端上传的文件
    netcode 控制台项目生成exe文件
    C# UDP发送和接收
    C# 直播录制视频
    Vs2017 FrameWork EF Mysql Mvc 三层整合1
    Vs2017 FrameWork EF Mysql 控制台应用
    Vs2017 NetCode EF Mysql 控制台应用
    Vs2017 NetCode Mvc EF Mysql 整合2
  • 原文地址:https://www.cnblogs.com/loaderman/p/6435146.html
Copyright © 2011-2022 走看看