问题描述
我尝试了下面的代码来检查我的手机是否连接到无线网络,当我想知道我的手机是否连接到网络时它运行良好,但它无法提供有关互联网访问的信息......像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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!