Android under self-writing similar system WiFi management function implementation

Source: Internet
Author: User

Often asked about the WiFi management under the Android issue, asked about the WiFi operation, below I write how to implement Android-like Android system with WiFi management function steps, through this study, I believe that the operation of WiFi under Android, we basically can master:

First, talk about the interface layout

The interface is simple:

Top a refresh button and a WiFi switch

A ListView in the lower part, each line is a scanned WiFi message showing its SSID, encryption type, signal strength, whether it is connected

Second, about the access to the list

How do I get a WiFi scan list? The Getscanresult () method of the Wifimanager class can be obtained directly, but it is not recommended that you get it directly through Getscanresult () at the beginning of the code, the more scientific process is:

1, registered WiFi scan results change broadcast, monitor WiFi list change situation

2. Call Wifimanager's Startscan () method to request a rescan of nearby WiFi hotspot information

3, in the Broadcast receiver processing call Getscanresult () method, get WiFi scan results, shown in the ListView, it is important to note that a lot of places in order to seamlessly cover, will be laying multiple SSID the same wireless hotspot, in our obtained scanresult, Will each exist, but we observe the system's WiFi management, the same name of the list will be merged, so we also need to merge, with the same name, only show the strongest signal that is OK

The key code snippet is as follows:

private void Regwifireceiver () {Intentfilter labelintentfilter = new Intentfilter (); Labelintentfilter.addaction ( wifimanager.scan_results_available_action); labelintentfilter.setpriority (1000); Set priority, up to 1000context.registerreceiver (Wifiresultchange, labelintentfilter);}
</pre><pre name= "code" class= "java" ><pre name= "code" class= "Java" >//rejects duplicates in the SSID, Keep only the strongest signal in the same SSID list<scanresult> wifilist = Wifimanager.getscanresults ();  list<scanresult> newwiflist = new Arraylist<scanresult> (); Boolean isadd = True;if (wifilist! = null) {for (int i = 0; I < wifilist.size (); i++) {isadd = true;for (int j = 0; J < Newwiflist.size (); j + +) {if (Newwiflist.get (j). Ssid.equals (Wifilist.get (i). SSID) {Isadd = False;if (Newwiflist.get (j). Level < Wifilist.get (i), level) {//SSID is the same and new signal is stronger newwiflist.remove (j); Newwiflist.add (Wifilist.get (i)); break;}}} if (Isadd) Newwiflist.add (Wifilist.get (i));}}




Third, about the connection

Now that we've got the list, how do we connect?

We follow the following logic, and if the user clicks on a line, we handle the following logic:

1. If we are currently connected to this hotspot, a dialog box appears prompting the user to "forget" or "disconnect" (respectively, the Removenetwork () method and the Disconnect () method of the Wifimanager), ending the process

2. If this hotspot information is configured before this machine, a dialog box is displayed, prompting the user to "forget" or "Connect" (respectively, the Removenetwork () method of Wifimanager and the Enablenetwork () method), ending the process

3, if this hotspot is a hotspot with a password, pop-up password input box, prompting the user to enter a password, enter the password, connect the

4, if this hotspot no password, pop-up prompt box, prompt no password, may not be safe, let the user choose whether to connect


Iv. How to connect a native configured hotspot

For native configured hotspots, we need to get their netid and then Enablenetwork (NetID), but, directly like this, you will find that often cannot connect successfully, the system will often automatically connect to the currently connected hotspot, rather than our designated hotspot, The problem is that there is a priority attribute in the Hotspot profile (Wificonfiguration Class), and the system automatically connects to the top hotspot, so the problem is that we need to enablenetwork (NetID) before Adjust the priority of the netid corresponding hotspot in the system to the maximum of the configuration information


Well, with the above ideas and steps to complete similar system WiFi management function is very easy!


Welcome to AC qq:568626884


Need full demo friend, can go to my resources to download

Android under self-writing similar system WiFi management function implementation

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.