Introduction
In this article, I will show you how to Enable and Disable WI-FI in Android apps using Android Studio. The information that an application can access includes connected network's link speed, IP address, negotiation state, other network information. Applications can also scan, add, save, terminate, and initiate Wi-Fi connections.
Requirements
Steps should be followed
These are the steps to create a changed background and audio player Android app. I have included the source code below.
Step 1
Open Android Studio and start a new Android Studio Project.
Step 2
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
Step 3
Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role in running the application.
Step 4
Now, add the activity and click the "Next" button.
Step 5
Add activity name and click "Finish".
Step 6
Go to activity_main.xml. This XML file contains the designing code for the Android app in the activity_main.xml.
The XML code is given below
- <?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"
- tools:context=".MainActivity">
-
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"
- android:src="@drawable/wifi"
- android:layout_alignParentTop="true"
- android:layout_alignParentStart="true"
- android:layout_marginStart="14dp" />
-
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="76dp"
- android:text="Enable Wifi"
- android:layout_alignParentStart="true"
- android:layout_marginStart="28dp"
- android:layout_alignBottom="@+id/button2" />
-
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Disable Wifi"
- android:layout_marginBottom="52dp"
- android:layout_marginEnd="14dp"
- android:layout_alignParentBottom="true"
- android:layout_alignParentEnd="true" />
-
- </RelativeLayout>
Step 7
Go to Main Activity.java. This Java program is the backend language for Android.
The java code is given below
- package abu.wifi;
-
- import android.net.wifi.WifiManager;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Context;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class MainActivity extends Activity {
- Button enableButton,disableButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- enableButton=(Button)findViewById(R.id.button1);
- disableButton=(Button)findViewById(R.id.button2);
-
- enableButton.setOnClickListener(new OnClickListener(){
- public void onClick(View v){
- WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- wifi.setWifiEnabled(true);
- }
- });
-
- disableButton.setOnClickListener(new OnClickListener(){
- public void onClick(View v){
- WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- wifi.setWifiEnabled(false);
- }
- });
- }
- }
Step 8
We need to Enable and Disable WI-FI. So, add WI-FI state permission in an AndroidManifest.xml.
The AndroidManifest.xml Code is given below
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="abu.wifi">
- <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>
Step 9
Now, go to the menu bar and click "Make a project" or press ctrl+f9 to debug it for errors.
Step 10
Then, click the Run button or press shift+f10 to run the project. And choose the virtual machine and click OK.
Conclusion
We have successfully created the "Enabled and Disabled WI-FI" Android application using Android Studio.