如何检查 Honeycomb 上的网络可用性?
我正在尝试检查我的蜂窝应用程序上是否有可用的简单连接。我已经尝试了该函数的 2 个不同版本:
public boolean networkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
尽管
private boolean networkAvailable(){
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectionManager.getActiveNetworkInfo();
return activeNetwork != null;
}
这些版本在我的 Honeycomb 平板电脑上都不起作用,但无论我尝试调用它们,它们都会崩溃。在 3.2 上有没有一种特殊的方法可以做到这一点,或者......我错过了什么?
I'm trying to check if a simple connection is available on my honeycomb app. I've tried 2 different versions of the function:
public boolean networkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
and
private boolean networkAvailable(){
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectionManager.getActiveNetworkInfo();
return activeNetwork != null;
}
Neither of these work on my Honeycomb tablet though, they crash wherever I try to call them. Is there a particular way of doing this on 3.2 or...what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效。您甚至可以区分可用网络的类型。确保您拥有清单中声明的以下权限。
This should work. You can even differentiate the type of network available. Make sure you have the following permissions declared in the manifest.
试试这个:
Try This :