WifiManager class is used for developing android apps which can enable/disable Wifi on the android device.
Start normally by navigating to File–>New–>Project–>Android Project.In package explorer you can find all your projects in workspace.Select your current project to view its hierarchy.Open res folder and select layout folder.You will find main.xml already there by default which is the user interface or layout of the application.
By default there is a TextView which by default shows the name of Activity class.In layout ,you require one TextView which shows the Wifi status(On/Off) of the android device and two Buttons which are used to Turn On the WiFi if it is disabled and other is used to turn off the Wifi if it is disabled.The layout should look like as shown below:
Now coming to the programming logic,get the id’s of the TextView and two Buttons to handle events and manipulate them as required.This is done as shown below:
Button TurnWifion, TurnWifioff;
TextView wifistatus;
TurnWifion = (Button)findViewById(R.id.button1); //button1 is the name of first Button in layout
TurnWifioff = (Button)findViewById(R.id.button2); //button2 is the name of second Button in layout
Wifistatus = (TextView)findViewById(R.id.textView1); //textView1 is the name of the TextView in layout
To check whether Wifi on android device is On/Off WifiManager class is used.Firstly the System Services have to be retrieved as shown:
WifiManager wifi;
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
Now the object of WifiManager class wifi will contain the information about Wifi on the device.WiFi status on the device can be checked by the following condition.
if(wifi.isWifiEnabled())
{
wifistatus.setText(“Connected”); //If WiFi is on, enable “Turn WiFi off” button.
TurnWifioff.setEnabled(true); // Changing the status of the Buttons
TurnWifion.setEnabled(false);
}
else
{
wifistatus.setText(“Disconnected”);//If WiFi is off, enable “Turn WiFi on” button.
Turnwifion.setEnabled(true);
TurnWifioff.setEnabled(false);
}
To Enable/Disable android device Wifi from the application itself you can use the following logic:
if(Turnwifion.isPressed()) //If “Turn WiFi on” button is Pressed
{
wifi.setWifiEnabled(true);// Enabling WiFi
Turnwifioff.setEnabled(true);
Turnwifion.setEnabled(false);
wifistatus.setText(“Connected”);
}
if(Turnwifioff.isPressed()) //If “Turn WiFi off” button is Pressed
{
wifi.setWifiEnabled(false); //Disabling WiFi
Turnwifion.setEnabled(true);
Tunwifioff.setEnabled(false);
wifistatus.setText(“Disconnected”);
And the most important thing is to add the following permissions to your Android Manifest.xml file:
<uses-permission android:name=“android.permission.ACCESS_WIFI_STATE” />
<uses-permission android:name=“android.permission.INTERNET” />
<uses-permission android:name=“android.permission.CHANGE_WIFI_STATE”/>
<uses-permission android:name=“android.permission.UPDATE_DEVICE_STATS” />
<uses-permission android:name=“android.permission.WAKE_LOCK” />
These permissions are needed to allow the application to access Wifi state and change it accordingly.
Full Code for the Wifi Enable/Disable Application is given below:
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.TextView;
public class ConnectivitymanagerActivity extends Activity implements OnClickListener {
Button TurnWifion, TurnWifioff;
TextView wifistatus;
WifiManager wifi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TurnWifion = (Button)findViewById(R.id.button1);
TurnWifioff = (Button)findViewById(R.id.button2);
WifiStatus = (TextView)findViewById(R.id.textView1);
// Getting the WiFi Services
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
//Checking whether Wifi is on or off
if(wifi.isWifiEnabled())
{
wifistatus.setText(“Connected”);//If WiFi is on, enable “Turn WiFi off” button.
TurnWifioff.setEnabled(true);
TurnWifion.setEnabled(false);
}
else
{
wifistatus.setText(“Disconnected”);//If WiFi is off, enable “Turn WiFi on” button.
Turnwifion.setEnabled(true);
TurnWifioff.setEnabled(false);
}
Turnwifion.setOnClickListener(this);
Turnwifioff.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(Turnwifion.isPressed()) //If “Turn WiFi on” button is Pressed
{
wifi.setWifiEnabled(true);// Enabling WiFi
Turnwifioff.setEnabled(true);
Turnwifion.setEnabled(false);
wifistatus.setText(“Connected”);
}
if(Turnwifioff.isPressed()) //If “Turn WiFi off” button is Pressed
{
wifi.setWifiEnabled(false); //Disabling WiFi
Turnwifion.setEnabled(true);
Tunwifioff.setEnabled(false);
wifistatus.setText(“Disconnected”);
}
}
}
Share on Facebook
