如何检查android中特定网络类型的可用性?
我写了一个android应用程序,需要通过网络传输大量数据。为了保持较低的发票成本,我在配置中添加了一个开关,该开关应将应用程序切换到“仅限 wlan”模式。
不幸的是,我无法在我的应用程序中实现对 WLAN 连接的正确检查,基本思想是通过
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我的清单添加所需的权限(这应该足以满足此目的),并实现了一个在我的活动中访问的简单方法:
protected boolean checkForNetwork(Context context, boolean wlanOnly) {
// check via ACCESS_NETWORK_STATE
ConnectivityManager manager = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
if (!wlanOnly) {
return manager.getActiveNetworkInfo().isConnected();
} else {
// hmm...
}
}
我我很不确定 // 嗯... 部分内容是什么。
I wrote an android app, which requires large amount of data to be transfered via the network. To keep the invoices low, I added a switch in my config, which should switch the app to "wlan only" mode.
Unluckily I was unable to implement the correct check for a WLAN connection in my app, the basic idea was adding the needed permissions via
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in my manifest (which should suffice for this purpose) and implemented a simple method which is accessed in my activity:
protected boolean checkForNetwork(Context context, boolean wlanOnly) {
// check via ACCESS_NETWORK_STATE
ConnectivityManager manager = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
if (!wlanOnly) {
return manager.getActiveNetworkInfo().isConnected();
} else {
// hmm...
}
}
I'm pretty unsure what goes into the // hmm... part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
状态 wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
您可以在这里查看;-)
http://developer.android。 com/reference/android/net/wifi/WifiManager.html
State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
You can have a look here ;-)
http://developer.android.com/reference/android/net/wifi/WifiManager.html