Android Check if there is WiFi but no internet(Android 检查是否有 WiFi 但没有互联网)
问题描述
我正在编写一个程序,我需要检查三个状态:1. 如果我没有 WiFi,2. 如果我有 WiFi 但没有互联网连接(比如我打开路由器但拔下以太网电缆),以及3. 如果我有 WiFi 和互联网连接.然后我会在我的应用程序中更改图标的颜色以表示其中一种状态(红色、黄色或绿色).目前条件2不起作用,每当我拔下路由器上的电缆进行测试时,图标颜色都会从绿色变为红色.
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();
}
推荐答案
这样检查wifi是否可用
Check if wifi is available like this
功能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
功能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;
}
条件
- 如果
function 1
返回 false --> 将颜色更改为 RED - 如果
function 1
返回 true 并且function 2
返回 false --> 将颜色更改为黄色 - 如果两个函数都返回 true --> 将颜色更改为绿色
- 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 但没有互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 检查是否有 WiFi 但没有互联网
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01