如何通过编程关闭 Android 设备上的 WiFi?

在按下“关闭屏幕”按钮后,我需要关闭 WiFi 一会儿。我的平板电脑需要这个应用程序,因为有时候我会忘记关掉 WiFi,这会让电池快速放电。它的寿命是没有 WiFi 的10倍以上。 是否有可用的解决方案,如。APK?我可以追踪屏幕何时关闭和5分钟经过?我可以通过程序关闭 Android 设备上的 WiFi 吗?怎么做到的?

121395 次浏览

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.