如何以编程方式检查Android的互联网连接的availibilty? [英] How to programmatically check availibilty of internet connection in Android?
问题描述
我要检查编程是否有android手机/仿真器连接到互联网。所以,一旦我相信,互联网连接是present然后我会打个电话到互联网。
I want to check programmatically whether there is an internet connection in android phone/emulator. So that once I am sure that an internet connection is present then I'll make a call to the internet.
因此,它像嘿仿真器如果你有一个互联网连接,那么请打开这个网页,否则doSomeThingElse();!
So its like "Hey emulator! If you have an internet connection, then please open this page, else doSomeThingElse();"
希望得到您的想法。我会强烈AP preciate快速反应我很早就需要它。
hope you get the idea. I would highly appreciate a quick response I need it quite early.
推荐答案
我实现了自己的方式:
/*
* isOnline - Check if there is a NetworkConnection
* @return boolean
*/
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
return false;
}
}
请注意,这是一个NetworkConnection-检查。如果有一个NetworkConnection它不必是一个InternetConnection
Be aware of that this is a NetworkConnection-Check. If there is a NetworkConnection it doesn't have to be a InternetConnection.
这篇关于如何以编程方式检查Android的互联网连接的availibilty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!