How to enable and disable WiFi programatically in android

In this tutorial we will learn how to handle WiFi through our application or how to do enable and disable WiFi programatically in android. First step is to have the following permission in your AndroidManifest.xml file .

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

Note that if target version is greater than 22 you need to get the permissions on runtime.

Refer Android Request permissions dynamically example , if you are not familiar to request permissions on run time in android.

Using the following code you can check whether WiFi is already enabled, if not it will enable WiFi.

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled()){

Toast.makeText(getApplicationContext(),"Wifi is already Enabled",Toast.LENGTH_SHORT).show();

}else {
wifi.setWifiEnabled(true);
}

Using the following code you can check whether WiFi is already disabled, if not it will disable WiFi.

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(!wifi.isWifiEnabled()){

Toast.makeText(getApplicationContext(),"Wifi is already Disabled",Toast.LENGTH_SHORT).show();

}else {
wifi.setWifiEnabled(false);
}

Let us see an example of enable/disable WiFi app in android. This example will contain two Buttons Enable Wifi and Disable Wifi

Let us start by adding the following code to your layout file

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:orientation="vertical">

<Button
android:id="@+id/btn_enable_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable Wifi"/>

<Button
android:id="@+id/btn_disable_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable Wifi"/>

</LinearLayout>

Now simply add the following code to your activity’s class file

public class WifiActivity extends AppCompatActivity {

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

Button btn_enable_wifi = (Button) findViewById(R.id.btn_enable_wifi);
Button btn_disable_wifi = (Button) findViewById(R.id.btn_disable_wifi);

btn_enable_wifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled()){

Toast.makeText(getApplicationContext(),"Wifi is already Enabled",Toast.LENGTH_SHORT).show();

}else {
wifi.setWifiEnabled(true);
}
}
});

btn_disable_wifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(!wifi.isWifiEnabled()){

Toast.makeText(getApplicationContext(),"Wifi is already Disabled",Toast.LENGTH_SHORT).show();

}else {
wifi.setWifiEnabled(false);
}
}
});
}

}

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *