Android的检查,如果有WIFI,但没有互联网

编程入门 行业动态 更新时间:2024-10-27 14:32:02
本文介绍了Android的检查,如果有WIFI,但没有互联网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我写一个程序,我需要检查三种状态:1。如果我有没有WiFi,2。如果我的WiFi,但没有互联网连接(比如,如果我把我的路由器,但拔掉网线),以及3.如果我有无线网络和互联网连接。然后,我会改变图标的​​颜色在我的应用程序,以重新present这些状态(红色,黄色或绿色)之一。目前条件2不工作,任何时候我拔掉数据线我的路由器进行测试时,该图标的颜色从绿色变为红色。

公共静态无效的兴奋剂(上下文的背景下){         字符串googleUrl =htt​​ps://www.google;         ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);         尝试 {             的HttpParams httpParameters =新BasicHttpParams();             HttpConnectionParams.setConnectionTimeout(httpParameters,TIMEOUT_C​​ONNECTION);             HttpConnectionParams.setSoTimeout(httpParameters,TIMEOUT_SOCKET);             HttpClient的客户端=新DefaultHttpClient(httpParameters);             如果(L)Log.i(TAG,叫:+网址);             HTTPGET getGoogle = getHttpGet(googleUrl);             HTT presponse responseGoogle = client.execute(getGoogle);             如果(responseGoogle!= NULL){                  connectionIconView.setIcon(R.drawable.green_wifi);             }             否则,如果(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)!= NULL){                 connectionIconView.setIcon(R.drawable.yellow_wifi);             }             其他 {                  connectionIconView.setIcon(R.drawable.red_wifi);             }         }赶上(例外五){             如果(L)Log.e(TAG,在HTTP调用错误);             e.printStackTrace();         }

解决方案

检查是否WiFi是可用这样的

功能1

私人布尔isWifiAvailable(){     ConnectivityManager connManager =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);     的NetworkInfo无线= connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);     返回wifi.isConnected(); }

在此,请像这样,如果互联网可

功能2

公共静态布尔isInternetAccessible(上下文的背景下){  如果(isWifiAvailable()){     尝试 {         HttpURLConnection的urlc =(HttpURLConnection类)(新的URL(www.google).openConnection());         urlc.setRequestProperty(用户代理,测试);         urlc.setRequestProperty(连接,关闭);         urlc.setConnectTimeout(1500);         urlc.connect();         返程(urlc.getResponse code()== 200);     }赶上(IOException异常E){         Log.e(LOG_TAG,无法检查网络连接,E);     } } 其他 {     Log.d(LOG_TAG,网络不可用!); } 返回false; }

条件

  • 如果功能1 返回false - >变为红色
  • 如果功能1 返回true和函数2 返回false - >变为黄色
  • 如果这两个函数返回true - >改变颜色为绿色
  • I am writing a program where I need to check three states: 1. If I have no WiFi, 2. if I have WiFi but no internet connection (like if I turn on my router but unplug the Ethernet cable), and 3. if I have WiFi and internet connection. I would then change color of a icon in my app to represent one of these states (red, yellow, or green). Currently condition 2 does not work, anytime I unplug the cable on my router for testing, the icon color changes from green to red.

    public static void doPing(Context context) { String googleUrl = "www.google"; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); try { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION); HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET); HttpClient client = new DefaultHttpClient(httpParameters); if (L) Log.i(TAG, "Calling: " + url ); HttpGet getGoogle = getHttpGet(googleUrl); HttpResponse responseGoogle = client.execute(getGoogle); if (responseGoogle != null){ connectionIconView.setIcon(R.drawable.green_wifi); } else if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null){ connectionIconView.setIcon(R.drawable.yellow_wifi); } else { connectionIconView.setIcon(R.drawable.red_wifi); } } catch(Exception e) { if (L) Log.e(TAG, "Error during HTTP call"); e.printStackTrace(); }

    解决方案

    Check if wifi is available like this

    function 1

    private boolean isWifiAvailable() { ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return wifi.isConnected(); }

    After this, check like this if internet is available

    function 2

    public static boolean isInternetAccessible(Context context) { if (isWifiAvailable()) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL("www.google").openConnection()); urlc.setRequestProperty("User-Agent", "Test"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); urlc.connect(); return (urlc.getResponseCode() == 200); } catch (IOException e) { Log.e(LOG_TAG, "Couldn't check internet connection", e); } } else { Log.d(LOG_TAG, "Internet not available!"); } return false; }

    Conditions

  • If function 1 returns false --> change color to RED
  • If function 1 return true and function 2 returns false --> change color to YELLOW
  • If both functions return true --> change color to GREEN
  • 更多推荐

    Android的检查,如果有WIFI,但没有互联网

    本文发布于:2023-11-27 17:01:27,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1638848.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:互联网   Android   WIFI

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!