Open In App

WifiManager in Android

Last Updated : 23 Dec, 2021
Like Article

WifiManager is the class related to the operations done with wifi. This class provides the primary API for managing all aspects of Wi-Fi connectivity. It deals with several categories of items:

  1. The list of available configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
  2. The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
  3. Results of access point scans, containing enough information to make decisions about what access point to connect to.
  4. It defines the names of various Intent actions that are broadcast upon any sort of the change in the Wi-Fi state.

Wifi of the android can be switched on manually or via the app. This article is all about making the app turn on the wifi without the user’s intervention. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

There is no need to change anything inside the activity_main.xml file.

Step 3: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


public class MainActivity extends AppCompatActivity 
    private WifiManager wifiManager;
    protected void onCreate(Bundle savedInstanceState)
        // wifiManager gets the Application
        // context and wifi services
        wifiManager=(WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        // wifi services are switched on
            // we check if wifi is connected to any of the connections
            String ip=Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
            // the IP address of the connection is shown in the toast message
            Toast.makeText(getApplicationContext(),"SUCCESSFULLY CONNECTED TO "+ip,Toast.LENGTH_SHORT).show();
        catch (Exception e) 
            // print error if unsuccessful to connect


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads