How to programmatically turn off WiFi on Android device?
You need the following permissions in your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
Then you can use the following in your activity class:
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);
Use the following to check if it's enabled or not
boolean wifiEnabled = wifiManager.isWifiEnabled()
You'll find a nice tutorial on the subject on this site.
Android: How to Enable/Disable Wifi or Internet Connection Programmatically
I know of enabling or disabling wifi:
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
where status may be true
or false
as per requirement.
Edit:
You also need the following permissions in your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Is it possible to programmatically enable wifi on Android 10 devices?
No, This is not possible to enable
or disable
Wi-Fi programmatically
from Android-10
API level 29
[Until google provides an alternative solution].
For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect.
If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.
There is an issue 128554616 which already has been created in google issuetracker
forum. You can see there for any updated info.
Disabling wifi in android programmatically
You need to add this permission in your manifest file.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
Then add this code in your java file.
you need to add two buttons,one for enabling and one for disabling.
final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
enableWifi.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(!wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(true);
Toast.makeText(LogTest.this,"Wifi Enabled",Toast.LENGTH_SHORT).show();
}
}
});
disableWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(LogTest.this,"Wifi Disabled",Toast.LENGTH_SHORT).show();
}
}
});
Use the following to check if it's enabled or not
boolean wifiEnabled = wifiManager.isWifiEnabled();
android turning on wifi programmatically
You need to create wifiLock with WIFI_MODE_FULL_HIGH_PERF
mode, based on the docs it will only work with the following constraints:
- The lock is only active when the device is connected to an access point.
- The lock is only active when the screen is on.
- The lock is only active when the acquiring app is running in the foreground.
Android - How to turn off Wifi-direct
Use WifiP2pManager.removeGroup(Channel channel, ActionListener listener) to leave the group, it will stop the Wifi Direct on your device.
http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html
Android WifiManager not enabling/disabling WiFi state
This API is no longer supported when targeting Android 10 or higher.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
Instead, you should use the Settings.Panel API to present a system UI allowing users to enable or disable Wi-Fi.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivity(Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY))
}
Related Topics
How to Create a User-Defined Function in SQLite
Difficulties with Calling an Android Ndk Function from Directly Delphi
Facebook Key Hash Does Not Match Any Stored Key Hashes
Get the Id of a Drawable in Imageview
What Is Exactly a "Clock Tick" in the Context of Android CPU Usage
Why Does Contentresolver.Requestsync Not Trigger a Sync
Getrunningtasks Doesn't Work in Android L
Android Studio Gradle Configuration with Name 'Default' Not Found
System Is Returning Error 127 When Called from C++ in Linux
Getting Bitmap from Vector Drawable
Checkbox Unchecked When I Scroll Listview on Android
Android Failed to Start Emulator:Cannot Run Program
"Failed to Install the Following Android Sdk Packages as Some Licences Have Not Been Accepted" Error
Hide/Show Bottomnavigationview on Scroll
How to Get the Sender of an Intent
How to Set Up Android Sdk for Command Line Development on Linux