如何以编程方式关闭WiFi的Andr​​oid设备?方式、设备、Andr、WiFi

2023-09-11 20:29:28 作者:融化你眉间霜雪

我需要关闭WiFi的一段时间后,pressing关闭屏幕按钮。有必要为这个应用程序为我的平板电脑,因为有时我只是忘了关闭WiFi和这个放电电池的速度非常快。它生活10X +倍不到我会不带WiFi。 是否有任何解决方案可作为.apk文件?我可以跟踪屏幕关闭,5分钟过去了?我可以编程关闭WiFi的Andr​​oid设备?怎么样?

I need to turn off the WiFi a while after pressing the "Turn off the Screen" button. There is need for this app for my tablet because sometimes I just forget to turn off the WiFi and this discharges the battery very fast. It lives 10x+ times less than I would without WiFi. Is there any solution available as .apk? Can I track when screen turned off and 5 min elapsed? Can I programatically turn off WiFi on Android device? How?

推荐答案

您需要在您的清单文件中的以下权限:

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.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()

您会发现在this站点。