2012-12-10 155 views
2

檢查是否存在有效的WiFi連接是沒有問題的。但是,我怎樣才能確保只有這個WiFi連接用於網絡訪問?限制網絡訪問只通過WiFi

假定以下情形:

  • 我檢查,如果有效的WiFi連接存在(而且可能是我驗證是否正常工作的Internet連接存在過)
  • 現在這個無線網絡連接中斷
  • 我開始通過網絡傳輸數據,現在使用移動連接,因爲WiFi最近死亡

我該如何避免這種情況?如果WiFi連接存在

回答

3

檢查:

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

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添加這個方法()方法,並檢查它,如果網絡chanes,您將收到的onReceive通知(),然後你可以處理無論你想如願