Android - How to check internet Access, not just connectivity to wifi?(Android - 如何检查互联网访问,而不仅仅是连接到 wifi?)
问题描述
我尝试了下面的代码来检查我的手机是否连接到无线网络,当我想知道我的手机是否连接到网络时它运行良好,但它无法提供有关互联网访问的信息......像Pinging"任何网站.实际上我关注了很多链接但仍然没有答案,所以如果有人可以提供帮助,我将非常感激.
I tried the below code to check whether my mobile is connected to a wireless network and it works well when I want to know if my mobile is connected to the network, but it fails to give information about the internet access ... something like "Pinging" any website. Actually I followed many links but still no answer, so I'll be so thankful if anybody can help.
提前致谢.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast t = new Toast(getApplicationContext());
if (isInternetOn()) {
// INTERNET IS AVAILABLE, DO STUFF..
Toast.makeText(ConnectivityTestActivity.this,"Network is Available", Toast.LENGTH_LONG).show();
}
else {
// NO INTERNET AVAILABLE, DO STUFF..
Toast.makeText(ConnectivityTestActivity.this,"No Network Available", Toast.LENGTH_LONG).show();
}
}
public final boolean isInternetOn() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
//Toast.makeText(this, connectionType + " connected", Toast.LENGTH_SHORT).show();
return true;
} else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
return false;
}
return false;
}}
点击下面的链接,它包含 Ping google 服务器的一个很好的答案并获得结果
Follow below link it contains a great answer for Ping google server and get result
https://stackoverflow.com/a/16458623/1239911
推荐答案
其实我用了同样的函数isInternetOn()
但是我去掉了连接条件.它必须检查连接状态是否已连接以及是否正在尝试连接.这对我不起作用,所以我删除了连接状态检查,然后它起作用了.
Actually I used the same function isInternetOn()
but I removed the connecting condition.
It had to check the status of connection if connected or not and if it is trying to connect. This didn't work for me, so I removed connecting status checking and then it worked.
感谢所有回复.
public final boolean isInternetOn()
{
ConnectivityManager connec = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED )
{
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
//Toast.makeText(this, connectionType + " connected", Toast.LENGTH_SHORT).show();
return true;
}
else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED )
{
return false;
}
return false;
}
这篇关于Android - 如何检查互联网访问,而不仅仅是连接到 wifi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android - 如何检查互联网访问,而不仅仅是连接到 wifi?
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01