After checking the information on the Internet, the system will shut down the WiFi in about two minutes after the phone screen is closed and other applications are not using WiFi, make WiFi sleep.
This approach is conducive to saving power and extending the battery life.
Android provides a wifilock lock for WiFi, which can prevent WiFi from going to sleep and keep WiFi active. This lock is suitable for downloading a large file.
Wifilock allows an application to keep the Wi-Fi radio awake.
1. Create a wifilock
1.
/***** @ Param lockname * Lock name * @ return wifilock */Public wifilock createwifilock (string lockname) {wifilock = wifimanager. createwifilock (lockname); Return wifilock ;}
2.
/***** @ Param lockname * Lock name * @ Param locktype ** wifi_mode_full = 1 <br/> * scan, automatically try to connect to a previously configured point <br/> * wifi_mode_scan_only = 2 <br/> * only scans left <br/> * wifi_mode_full_high_perf = 3 <br/> * Based on the first mode, best Performance <br/> * @ return wifilock */Public wifilock createwifilock (string lockname, int locktype) {wifilock = wifimanager. createwifilock (locktype, lockname); Return wifilock ;}
2. Add locks to wifi
/*** Lock */Public void lockwifi () {wifilock. Acquire ();}
3. release locks for WiFi
/*** Release lock */Public void releaselock () {If (wifilock. isheld () {wifilock. Release ();}}
4. Determine whether WiFi is locked or not
/*** Determine whether the WiFi lock holds ** @ return */Public Boolean isheld () {return wifilock. isheld ();}