How to switch Wifi On and Off automatically or remotely in your Android App? – Complete Source Code

This video shows the steps to create an Android App which can be used to control the state of your Wifi between ON and OFF. The concept shown in this video is very simple. However, the use cases to control the state of the wifi could be many which has been briefly discussed in this video. Some of the use cases could be to control the wifi remotely by sending the command over channels such as SMS, firebase database update, etc. It can also be controlled at a pre-set time using some kind of timer or alarm clock concept.

We hope you like this video. For any query, suggestions or appreciations we will be glad to hear from you at: programmerworld1990@gmail.com

Source Code:

package com.example.mywificontrolapp;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
private WifiManager wifiManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
}

public void WifiOnButton(View view){
wifiManager.setWifiEnabled(true);
}

public void WifiOffButton(View view){
wifiManager.setWifiEnabled(false);
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mywificontrolapp”>

<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>

Leave a Reply

%d bloggers like this: