Android examples for android.net:ConnectivityManager
Try to connect to the specified network
import java.util.List; import android.net.ConnectivityManager; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; public class Main { private static final String TAG = "WifiUtils"; /**//from w ww. j av a2 s .com * Try to connect to the specified network. * * @param wifiConfiguration * The WiFi configuration for the specific network * @param wifiManager * The WiFi manager created in SocialWifi * @param connectivityManager * The connectivity manager created in Social Wifi * @return True if connection was successful, otherwise false */ public static boolean connect(WifiConfiguration wifiConfiguration, WifiManager wifiManager, ConnectivityManager connectivityManager) { boolean connected = false; int inet = wifiManager.addNetwork(wifiConfiguration); if (inet > 0) { List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); for (WifiConfiguration i : list) { if (i.SSID != null && i.SSID.equals(wifiConfiguration.SSID)) { wifiManager.disconnect(); boolean b = wifiManager.enableNetwork(inet, true); boolean c = wifiManager.reconnect(); connected = b && c; break; } } } return connected; } }