仅通过WiFi限制网络访问

时间:2012-12-10 18:19:59

标签: android networking wifi

检查是否存在有效的WiFi连接是没有问题的。但是,我怎样才能确保只有这个WiFi连接用于网络访问?

假设以下情况:

  • 我检查是否存在有效的WiFi连接(可能是我验证是否存在有效的Internet连接)。
  • 现在此WiFi连接中断
  • 我开始通过网络传输数据,现在使用移动连接因为最近WiFi死了

我该如何避免?

3 个答案:

答案 0 :(得分:3)

检查是否存在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();
    }
}

请阅读Managing Network Usage了解详细解决方案

答案 1 :(得分:0)

您可以为网络更改添加广播接收器。因此,无论何时wifi被中断,您都会收到通知,您可以按照自己的意愿处理。您可以在以下链接http://developer.android.com/training/basics/network-ops/managing.html

中找到更多详细信息

答案 2 :(得分:0)

您可以通过此方法检查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()的通知,然后您可以处理您想要的任何内容你的愿望