我写一个程序,我需要检查三种状态:1。如果我有没有WiFi,2。如果我的WiFi,但没有互联网连接(比如,如果我把我的路由器,但拔掉网线),以及3.如果我有无线网络和互联网连接。然后,我会改变图标的颜色在我的应用程序,以重新present这些状态(红色,黄色或绿色)之一。目前条件2不工作,任何时候我拔掉数据线我的路由器进行测试时,该图标的颜色从绿色变为红色。
公共静态无效的兴奋剂(上下文的背景下){ 字符串googleUrl =https://www.google; ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 尝试 { 的HttpParams httpParameters =新BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters,TIMEOUT_CONNECTION); 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; }
条件
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
更多推荐
Android的检查,如果有WIFI,但没有互联网
发布评论