【发布时间】:2015-01-16 16:10:26
【问题描述】:
我正在编写一个程序,我需要检查三个状态:1. 如果我没有 WiFi,2. 如果我有 WiFi 但没有互联网连接(比如我打开路由器但拔下以太网电缆),以及3. 如果我有 WiFi 和互联网连接。然后我会在我的应用程序中更改图标的颜色以表示其中一种状态(红色、黄色或绿色)。目前条件2不起作用,每当我拔下路由器上的电缆进行测试时,图标颜色都会从绿色变为红色。
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();
}
【问题讨论】:
标签: android connection wifi