Home » Android

How to Switch on WIFI service in Android?

In this article, we are going to learn about WiFi manager and the process to use its services.
Submitted by Manu Jemini, on February 22, 2018

In the example below, we are going to use the WifiManager and Toast class to turn on and toast a result and also turn off it.

To implement this in your program you will need to import: android.net.wifi.WifiManager, android.widget.Toast in your java file.

Then create a single layout file with a single button, and it should be something like this:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Click here to Switch on the WIFI"
	android:id="@+id/button"
	android:layout_alignParentTop="true"
	android:layout_centerHorizontal="true"
	android:layout_marginTop="163dp"
	android:onClick="switchon"/>

In the Button above, we have put an onClick property and set it to switchon function which is in the java File. We assign on click function directly like this: android:onClick=" switchon ".

First of all, we will make an instance of WifiManager class and store it in the local variable like this:

WifiManager wifi =(WifiManager) getSystemService(Context.WIFI_SERVICE);

After that we are turning the WiFi on and making a toast to show that it works.

wifi.setWifiEnabled(true);

1) Java file:

package com.example.hp.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.Toast;


public class MainActivity extends AppCompatActivity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	public void switchon(View v){
		WifiManager wifi =(WifiManager) getSystemService(Context.WIFI_SERVICE);
		wifi.setWifiEnabled(true);
		Toast.makeText(MainActivity.this,"WiFi has been shitched on",Toast.LENGTH_LONG).show();
	}
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to Switch on the WIFI"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="163dp"
        android:onClick="switchon"/>
</RelativeLayout>

3)Android Manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <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: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>

Output

Switch on WIFI service in Android 1
Switch on WIFI service in Android 2

Output

Switch on WIFI service in Android 3






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.