Here, I give a function
1 Public BooleanSale_connect_check (wificonfiguration wcg,context Context) {2 Booleanjudge_2 =false;//Boolean variable of double judgment3 intWcgid = Wifimanager.addnetwork (WCG);//Gets the network ID that was used4 BooleanJudge = Wifimanager.enablenetwork (Wcgid,true);//on the line to operate, note, many online even WiFi demo, and finally it is connected with the5 //here to illustrate, even if the return is true, it will not necessarily be connected, it will only go to the operation of the Wi-Fi6 Try{7LOG.D ("tag", "before sleep here");8Thread.Sleep (4000);//The thread pauses for 4 seconds, because the link WiFi is a process, we have to give the above operation time to do the connection9Connectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (context.connectivity_service);TenNetworkinfo Wifinetworkinfo =Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); One if(Wifinetworkinfo.isconnected ()) {//At this point, here again to determine whether to connect, if 4 seconds enough, set their own ALOG.D ("tag", "is Do Connect"); -judge_2 =true;// - } the}Catch(Exception e) { -LOG.D ("tag", e.tostring ()); - } -Save_check_sale_judge = Judge && judge_2;//2 of them are true . + returnSave_check_sale_judge;//it's connected. -}
How to tell if the device is connected to WiFi