Android的检查,如果有WIFI,但没有互联网 [英] Android Check if there is WiFi but no internet
问题描述
我写一个程序,我需要检查三种状态:1。如果我有没有WiFi,2。如果我的WiFi,但没有互联网连接(比如,如果我把我的路由器,但拔掉网线),以及3.如果我有无线网络和互联网连接。然后,我会改变图标的颜色在我的应用程序,以重新present这些状态(红色,黄色或绿色)之一。目前条件2不工作,任何时候我拔掉数据线我的路由器进行测试时,该图标的颜色从绿色变为红色。
公共静态无效的兴奋剂(上下文的背景下){
字符串googleUrl =https://www.google.com;
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(http://www.google.com).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 = "https://www.google.com";
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("http://www.google.com").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 andfunction 2
returns false --> change color to YELLOW - If both functions return true --> change color to GREEN
这篇关于Android的检查,如果有WIFI,但没有互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!