【问题标题】:how can i check my device is connected with internet or not如何检查我的设备是否已连接互联网
【发布时间】:2018-02-28 05:38:43
【问题描述】:

如何检查我的设备是否已连接到互联网。

我有 android 设备,因为我启动了 wifi 数据,但 wifi 数据用于本地网络,但它仍然显示您的互联网连接已打开,而实际上没有。

那我如何通过检查来检查这个类的连接类
我会给你上网检查的代码

public class NetworkUtil {

    public static int TYPE_WIFI = 1;

    public static int TYPE_MOBILE = 2;

    public static int TYPE_NOT_CONNECTED = 0;

    public static int getConnectivityStatus(Context context) {

        if (Build.VERSION.SDK_INT >= 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {

            return TYPE_WIFI;

        } else {

            return TYPE_NOT_CONNECTED;
        }

    }

    public static String getConnectivityStatusString(Context context) {
        int conn = NetworkUtil.getConnectivityStatus(context);
        String status = null;
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "Wifi enabled";
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = "Mobile data enabled";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
        }
        return status;
    }

    public static boolean isInternetWorking() {
        boolean success = false;
        try {
            URL url = new URL("https://google.com");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(10000);
            connection.connect();
            success = connection.getResponseCode() == 200;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }
} 

【问题讨论】:

标签: android connection networkmanager


【解决方案1】:

请使用以下方法检查互联网连接。

private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null;
}

【讨论】:

  • 谢谢你的回答,但它只是在实际互联网工作时检查我需要的 wifi 连接
【解决方案2】:

0 表示没有互联网 1 表示有互联网

private int checkInternetConnectivity() {
        ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
        if (netInfo == null) {
            return 0;
        } else {
            return 1;
        }
    }

【讨论】:

  • 谢谢你的回答,但它只是在实际互联网工作时检查我需要的 wifi 连接
【解决方案3】:

在课堂上使用这种方式

     NetworkUtil networkUtil=new NetworkUtil(this)
     if(networkUtil.isInternetWorking()){
     Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
     }else{
     Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show();
     }

【讨论】:

    【解决方案4】:

    您可以通过此代码轻松检查 Internet 连接:

    ConnectivityManager ConnectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = ConnectionManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected() ) {
    
                 //Connected
            } else {
                //Not connected
            }
    

    别忘了在 Manifest 中声明权限

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

      【解决方案5】:

      创建一个类:-

      public class ConnctionDetector 
      
      {
      
      ConnectivityManager connectivityManager;
      
      Context context;
      
      NetworkInfo info;
      
      public ConnctionDetector(Context context)
      {
          this.context=context;
      }
      public boolean checkin(Context context)
      {
          boolean flag = false;
          try {
              connectivityManager = (ConnectivityManager) 
      context.getSystemService(Context.CONNECTIVITY_SERVICE);
              info = connectivityManager.getActiveNetworkInfo();
      
              if (info.getType() == ConnectivityManager.TYPE_WIFI)
              {
                  System.out.println(info.getTypeName());
                  flag = true;
              }
              if (info.getType() == ConnectivityManager.TYPE_MOBILE)
              {
                  System.out.println(info.getTypeName());
                  flag = true;
              }
          } catch (Exception exception) {
              System.out.println("Exception at network connection....."
                      + exception);
          }
          return flag;
      }
      }
      

      以及如何检查:-

      boolean checkconnection = new 
      ConnctionDetector(getApplicationContext()).checkin(getApplicationContext());
      
      if (!checkconnection) {
                      Toast.makeText(getApplicationContext(), "No Internet 
      Connection", Toast.LENGTH_SHORT).show();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2018-01-29
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2019-09-03
        相关资源
        最近更新 更多