Android turn on and turn off wifi


In this Android tip, I am going to show you how to turn on and turn off Wifi programmatically. Android provides WifiManager class that is able to manage all aspects of Wifi connectivity. Wifi is a system service. To get an instance of the WifiManager, you need to pass Context.WIFI_SERVICE to the getSystemService() method of your current activity. An application that needs to manage Wifi connectivity requires the following user permissions: CHANGE_WIFI_STATE and ACCESS_WIFI_STATE. These permissions have to be declared in AndroidManifest.xml file of your current working project.

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

The WifiManager class has a method called setWifiEnabled() that allows you to turn on or turn off the Wifif connection on an Android device. By passing true to the method, the Wifi will be turned on. Otherwise, it is turned off. It is a good idea to check whether the Wifi is turned on or off before you turn it off or on. A convenient method to do the task is isWifiEnabled(). The isWifiEnabled() method returns true if the Wifi is already turned on. Otherwise, it returns false.
Now to have example application on using WifiManager to turn the Wifi connection on and off, you create a new project. Then add two buttons to the activity_main.xml file.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context="com.example.wifiproject.MainActivity" >

  <Button
android:id="@+id/btturnon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Turn on Wifi"
android:onClick="turnonWifi"
/>
  <Button
android:id="@+id/btturnoff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Turn off Wifi"
android:onClick="turnoffWifi"
/>

</LinearLayout>


In the MainActivity class, you create two methods: turnonWifi() and turnoffWifi(). The turnonWifif() is called when you click the button to turn on the Wifi and the turnoffWifif() method turns off the Wifi when it is clicked.

package com.example.wifiproject;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity{

WifiManager wifi;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     // get WifiManager instance
     wifi = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
    
   }

   public void turnonWifi(View view){
   if (!wifi.isWifiEnabled()) {
   wifi.setWifiEnabled(true); // turn wifi on
   Toast.makeText(this, "Turn on Wifi",Toast.LENGTH_LONG).show();
   }
   }
   public void turnoffWifi(View view){
   if (wifi.isWifiEnabled()) {
   wifi.setWifiEnabled(false); // turn wifi off
   Toast.makeText(this, "Turn off Wifi",Toast.LENGTH_LONG).show();
   }
   }

}


Before you run the example application, don't forget to add the permissions mentioned above in the AndroidManifest.xml file of your project.

Android-turn-on-and-turn-off-wifi

Posted by: Dara | post date: 12-14-2014 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.