Programmatically turn ON/OFF WiFi on Android device
WifiManager provides the primary API for managing all aspects of WI-FI connectivity.we can get WifiManager instance through Context.getSystemService(Context.WIFI_SERVICE).
WifiManager is used to find the currently active Wi-Fi network and change the Wi-Fi State.
Create Android Project with name as "TurnOnWIFI" with package name "com.etr.turnonwifi". Create Activity with name as "MainActivity" and it will turn ON/OFF Wi-Fi. This is the code for MainActivity.
Paste below code to "activity_main.xml"
Paste below code to "strings.xml"
Paste below code to "AndroidManifest.xml"
You can download source code here
WifiManager is used to find the currently active Wi-Fi network and change the Wi-Fi State.
Create Android Project with name as "TurnOnWIFI" with package name "com.etr.turnonwifi". Create Activity with name as "MainActivity" and it will turn ON/OFF Wi-Fi. This is the code for MainActivity.
package com.etr.turnonwifi;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button start, stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start_wifi);
stop = (Button) findViewById(R.id.stop_wifi);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
WifiManager wifi = (WifiManager) MainActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
Toast.makeText(MainActivity.this, "Turn ON WIFI",
Toast.LENGTH_LONG).show();
}
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WifiManager wifi = (WifiManager) MainActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
Toast.makeText(MainActivity.this, "Turn OFF WIFI",
Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
Log.v("MainActivity Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button start, stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start_wifi);
stop = (Button) findViewById(R.id.stop_wifi);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
WifiManager wifi = (WifiManager) MainActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
Toast.makeText(MainActivity.this, "Turn ON WIFI",
Toast.LENGTH_LONG).show();
}
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WifiManager wifi = (WifiManager) MainActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
Toast.makeText(MainActivity.this, "Turn OFF WIFI",
Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
Log.v("MainActivity Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Paste below code to "activity_main.xml"
<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">
<Button
android:id="@+id/start_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/start"
android:layout_marginTop="50dp" />
<Button
android:id="@+id/stop_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/stop"
android:layout_marginTop="50dp" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/start_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/start"
android:layout_marginTop="50dp" />
<Button
android:id="@+id/stop_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/stop"
android:layout_marginTop="50dp" />
</LinearLayout>
Paste below code to "strings.xml"
<string name="start">Turn ON WIFI</string>
<string name="stop">Turn OFF WIFI</string>
<string name="stop">Turn OFF WIFI</string>
Paste below code to "AndroidManifest.xml"
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
You can download source code here
Comments
Post a Comment