Android get around WiFi hotspot service _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.