title: Programmatically connecting to a specific wifi
body: <p>I would like to connect to specific wifi programmatically in android. I referred lot of links. But still not yet achieve the goal. I am following below code to connect specific wifi.</p>
  
  <pre><code> WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration configuration = new WifiConfiguration();
                                      configuration.SSID = String.format("\"%s\"", "ssid");
                                      configuration.preSharedKey = String.format("\"%s\"", "paasword");
                                      configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                                      int netId = wifiManager.addNetwork(configuration);
                                      wifiManager.disconnect();
                                      wifiManager.enableNetwork(netId,true);
                                      wifiManager.reconnect();
  </code></pre>
  
  <p>When I was try to run this code stuff.. wifi connection will be disconnected and again automatically connected previous wifi network.I am using redmi 6a mobile(version 9) for testing purpose.
  Can any one guide me how to resolve this one.</p>
  
  <p>Thanks in advance.</p>