In the actual development we will often need to get around WiFi hotspots, recently done this demo, write to share with you. The general idea is this, first wifimanger get WiFi services, and then store the results in arraylist<scanresult>, OK, the specific content you can see the code directly:
Mainactivity.java:
public class Mainactivity extends appcompatactivity {arraylist<scanresult> list;
Store a list of the surrounding WiFi hotspot objects Wifimanager Wifimanager;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); Wifimanager = (Wifimanager) getsystemservice (Wifi_service);
Obtain the system WiFi Service list = (arraylist<scanresult>) wifimanager.getscanresults ();
Sortbylevel (list);
Init ();
private void Init () {TextView tv1= (TextView) Findviewbyid (R.ID.TV1);
TextView tv2= (TextView) Findviewbyid (R.ID.TV2);
TextView tv3= (TextView) Findviewbyid (R.ID.TV3); if (list.get (0). SSID!= null && list.get (1). SSID!= null) {Tv1.settext ("strongest signal" +list.get (0).
SSID); Tv2.settext ("Signal second digit:" +list.get (1).
SSID);
Tv3.settext ("Total" +list.size () + "WiFi"); }///To sort the search WiFi from strong to weak based on signal strength private void sortbylevel (arraylist<scanresult> list) {for (int i=0;i<li St.size (); i++ for (int j=1;j<list.size (), j + +) {if (List.get (i). Level<list.get (j). Level)//level property is strength
{Scanresult temp = null;
temp = List.get (i);
List.set (I, List.get (j));
List.set (J, temp);
}
}
}
}
Layout file Activity_main.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<textview
android:id= "@+id/tv1" android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:text= "Hello world!"/>
<textview
android : id= "@+id/tv2"
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
Android : text= "Hello world!"/>
<textview
android:id= "@+id/tv3"
android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:text= "Hello world!"/>
</LinearLayout>
OK, that's the way it is.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.