Wi-Fi detection for Android web Apps

Source: Internet
Author: User

This chapter is mainly used for the automatic background detection of the component application WiFi signal is not.

The main function is: The application in the process of starting the use of the full detection of WiFi signal, when the WiFi signal will pop up a toast prompt, when the WiFi signal disappears will be prompted, other time state is not prompted.

function is relatively simple, but the small part is the first time to start Android, put their own ideas written down, the big God do not spray.

First, the main idea is divided into three steps:

1), the time task is called in the OnCreate method, the time is set to 5 seconds, that is, every 5 seconds the detection runs once.

2), when the connection is detected, send a signal, when the connection is disconnected, send a non-connected signal

3), handler processing, receive message, according to the msg.what response to different processing, popup toast prompt.


The classification code is as follows:

in OnCreate:

Add access to WiFi in the manifest file first

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>

Then build the Wifimanger object in OnCreate:

Wifimanager Wifimanager = (wifimanager) getsystemservice (context.wifi_service

Next, create a timer object to perform a time task:

Timer timer = new timer (); Timer.schedule (new MyThread (Wifimanager), 1000, 5000);
This is set to 5s detection once


To build the Mythread class:

Private class MyThread extends TimerTask {Private Boolean conn;private Wifimanager manager;public MyThread (Wifimanager _m Anager) {This.manager = _manager;}  @Overridepublic void Run () {conn = this.manager.getWifiState () = = WIFIMANAGER.WIFI_STATE_ENABLED;IF (conn) {Message msg = New Message (); msg.what = 1;myhandler.sendmessage (msg);}       else        {message msg = new Message (); msg.what = 0;myhandler.sendmessage (msg);}}       }
The function of Mythread is to execute every 5 seconds and then get the status of WiFi in the run, if it is connected, send a message of what is 1, and if it is not connected, send a message of what is 0.


Execute the Handler.handlemessage (Message msg) method:

@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {Case 1:        showalerttrue ();        Break;case 0:showalertfalse ();d efault:break;}}
The Showalerttrue () and Showalertfalse () function is to execute a toast, which is not posted.

After writing the code to run, does show the prompt message, but there is a bad place, is the application during the execution of every 5 seconds to pop up the toast prompt, compromising the user experience, we need to send status changes when the prompt appears.

So change your mind and try to get information about state change:

My idea here is to create two objects of type int, denoted by historystate and CurrentState, which are used to represent the current and previous WiFi connection states, respectively.

There are three values for two variables, 1 for stateless, 1 for connection status, and 0 for disconnected state. (-1 mainly to give historystate, because the concept was not connected or disconnected before the first run)
When currentstate-historystate = 0 o'clock, indicates that no message is sent before and after two states, the flag variable defined is-1, indicating no change

When currentstate-historystate = 1 o'clock, indicates that the state is disconnected from the connection, at which point the flag variable is 1, indicating that a connection was generated.

When currentstate-historystae =-1, the state is connected to a break, at which time the flag variable is 0, indicating that a fracture has occurred.

Create a function to get the Currentstae:

public int getcurrentstate () {return this.manager.getWifiState () = = wifimanager.wifi_state_enabled? 1:0;}

Next we modify Mythread's Run method as follows:

@Overridepublic void Run () {if (historystate = =-1) {currentstate = Getcurrentstate (); if (currentstate = = 1) {   <spa n style= "white-space:pre" ></span>flag = 1;//represents a change, resulting in a connection change}else{flag = 0;//break change}<span style= "White-space: Pre "></span>historystate = CurrentState;        } else {currentstate = Getcurrentstate (); int result = CURRENTSTATE-HISTORYSTATE;IF (result = = 0) flag = -1;else if (result > 0) flag = 1;elseflag = 0;historystate = CurrentState;} if (flag = = 1) {Message msg = new Message (); msg.what = 1;myhandler.sendmessage (msg);} else if (flag = = 0) {Message msg = new Message (); msg.what = 0;}}

This completes the prompt implementation of the state exchange.
WiFi programming can be obtained from the system service using the Wifimangager object, and each run detects the current system service state and gets whether it is wifi-state-enabled.

To get additional WiFi information you can use Wifimanage.getconnectioninfo to get Wifiinfo object, you can get WiFi ID number, IP address, signal strength and other information, you can refer to the API documentation.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Wi-Fi detection for Android web Apps

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.