How to enable and disable the wi-fi in android

Android provides the features by which you can enable and disable the wi-fi in android.

In my prgram run the app it will show you the two buttns first is enable the wi-fi and second is disable the wi-fi.


Enable & Disable WI-FI:.

In this section i am going to show you how to enable and disable the wi-fi with practical example.

First open the android studio and create a project and give the name of your project like WifiDemo.

The screenshots of the program is as follows:

wifi image wifi image

You need to drag and drop the two Button from pallet. Open the activity_main.xml file and code look like this


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
<Button
    android:id="@+id/btnon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:text="Enable wifi"
    android:onClick="enableWifi"
    />
<Button
    android:id="@+id/btnoff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:text="Disable wifi"
    android:onClick="disableWifi"
    />

</LinearLayout>
       

Open the AndroidManifest.xml file and give the permission for wi-fi.The code is look like this

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.niitpc.myapplication">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
   

<application
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:roundIcon="@mipmap/ic_launcher_round"
   android:supportsRtl="true"
   android:theme="@style/AppTheme">
   <activity android:name=".MainActivity">
   <intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
   </activity>
   </application>

</manifest>
       

In AndroidManifest.xml file only write two line of code for permission.

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

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


Open the MainActivity java class and write code look like this.


package com.example.niitpc.myapplication;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity   {


WifiManager wifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate( savedInstanceState );
  setContentView(R.layout.activity_main );

  wifiManager=(WifiManager)getApplicationContext().getSystemService( Context.WIFI_SERVICE );

 }
public  void enableWifi(View v)
{
   wifiManager.setWifiEnabled( true );
   Toast.makeText( MainActivity.this,"Wifi enabled",Toast.LENGTH_LONG ).show();
}
public  void disableWifi(View v)
{
  wifiManager.setWifiEnabled( false );
  Toast.makeText( MainActivity.this,"Wifi disabled",Toast.LENGTH_LONG ).show();
 }
}




Explanation:

WifiManager wifiManager:Here i have declared the object wifiManager of WifiManager.

wifiManager=(WifiManager)getApplicationContext().getSystemService( Context.WIFI_SERVICE );:Here i have declared the object checker of ConnectionChecker class.

public void enableWifi(View v):Here i have created the eableWifi() that will enable the wi-fi.

wifiManager.setWifiEnabled( true ):Here i have called the setWifiEnabled() with the help of wifiManager object and the method take the one argument as true or false here i have given true that will enable the wi-fi.

Toast.makeText( MainActivity.this,"Wifi enabled",Toast.LENGTH_LONG ).show():This statement print the message wi-fi enbaled with the help of Toast.

public void disableWifi(View v):Here i have created another method disableWifi() that will disable the wi-fi.

wifiManager.setWifiEnabled( false ):Here i have called the setWifiEnabled() with the help of wifiManager object and the method take the one argument as true or false here i have given false that will disable the wi-fi.

Toast.makeText( MainActivity.this,"Wifi disabled",Toast.LENGTH_LONG ).show() :It will print the message Wifi disabled.

The enableWifi() must pass the android:onClick of the btnon button and disableWifi() to btnoff button:.


Wi-Fi disable & enable

how to disable & eanble wi-fi for video click here

back                     next