【发布时间】:2012-12-10 18:19:58
【问题描述】:
检查是否存在有效的 WiFi 连接没有问题。但是如何确保只有这个 WiFi 连接用于网络访问?
假设如下场景:
- 我检查是否存在有效的 WiFi 连接(我可能会验证是否也存在有效的 Internet 连接)
- 现在此 WiFi 连接已中断
- 我开始通过网络传输数据,现在使用移动连接,因为 WiFi 最近断开了
我怎样才能避免这种情况?
【问题讨论】:
标签: android networking wifi
检查是否存在有效的 WiFi 连接没有问题。但是如何确保只有这个 WiFi 连接用于网络访问?
假设如下场景:
我怎样才能避免这种情况?
【问题讨论】:
标签: android networking wifi
检查WiFi连接是否存在:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
Source,或者使用这个代码sn-p:
private static final String DEBUG_TAG = "NetworkStatusExample";
...
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);
添加 Listener 以检查 WiFi 是否仍处于启用状态:
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conn = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conn.getActiveNetworkInfo();
// Checks the user prefs and the network connection. Based on the result, decides whether
// to refresh the display or keep the current display.
// If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection.
if (WIFI.equals(sPref) && networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// If device has its Wi-Fi connection, sets refreshDisplay
// to true. This causes the display to be refreshed when the user
// returns to the app.
refreshDisplay = true;
Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show();
// If the setting is ANY network and there is a network connection
// (which by process of elimination would be mobile), sets refreshDisplay to true.
} else if (ANY.equals(sPref) && networkInfo != null) {
refreshDisplay = true;
// Otherwise, the app can't download content--either because there is no network
// connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there
// is no Wi-Fi connection.
// Sets refreshDisplay to false.
} else {
refreshDisplay = false;
Toast.makeText(context, R.string.lost_connection, Toast.LENGTH_SHORT).show();
}
}
详细解决方案请阅读Managing Network Usage
【讨论】:
您可以为网络更改添加广播接收器。因此,每当 wifi 中断时,您都会收到通知,您可以根据需要处理这种情况。您可以在以下链接中找到更多详细信息http://developer.android.com/training/basics/network-ops/managing.html
【讨论】:
你可以通过这种方法检查wifi是否中断?
ConnectivityManager networkManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = networkManager.getActiveNetworkInfo();
NetworkInfo wifi = networkManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isAvailable() && wifi.isConnected()) {
return true;
}else {
return false;
}
您可以将广播接收器添加到 lsten 以进行网络更改,您可以在 onReceive() 方法中添加此方法,并检查它,如果网络通道,您将在 onReceive() 上收到通知,然后您可以处理任何您想要的你的愿望
【讨论】: