检查是否存在有效的 WiFi 连接是没有问题的。但是如何确保只有这个 WiFi 连接用于网络访问?
假设以下场景:
- 我检查是否存在有效的 WiFi 连接(我可能会验证是否存在有效的 Internet 连接)
- 现在这个 WiFi 连接被中断了
- 我开始通过网络传输数据,现在使用移动连接,因为 WiFi 最近死了
我怎样才能避免这种情况?
检查是否存在有效的 WiFi 连接是没有问题的。但是如何确保只有这个 WiFi 连接用于网络访问?
假设以下场景:
我怎样才能避免这种情况?
检查是否存在 WiFi 连接:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
Source,或使用此代码段:
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);
添加监听器以检查 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();
}
}
请阅读管理网络使用以获得详细的解决方案
您可以添加广播接收器以进行网络更改。因此,每当 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() 上收到通知,然后您可以根据需要处理任何您想要的