Set the Android device to always enable WLAN during sleep
MainActivity is as follows:
Package cc. AB; import android. OS. bundle; import android. provider. settings; import android. app. activity;/*** Demo Description: * sets the device to always enable WLAN during sleep. ** reference: * 1 http://stackoverflow.com/questions/8652031/how-to-modify-wi-fi-sleep-policy-programmatically/8655070#8655070 * 2 http://blog.csdn.net/mrlixirong/article/details/24938637 * Thank you very much ** Note: * 1 here is the android. provider. settings. system. wi-Fi _ SLEEP_POLICY * instead of Settings. System. WIFI_SLEEP_POLICY !!!!!!!!!!!!!!!!!!!!!!! ** 2 Permissions
** 3 in actual projects, It is rude to set up WIFI so forcibly during sleep. * You can record the original WIFI policy and restore it after the logic is complete. */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); setWifiNeverSleep ();} private void setWifiNeverSleep () {int wifiSleepPolicy = 0; wifiSleepPolicy = Settings. system. getInt (getContentResolver (), android. provider. settings. system. WIFI_SLEEP_POLICY, Settings. system. WIFI_SLEEP_POLICY_DEFAULT); System. out. println ("---> the Wifi sleep policy value WIFI_SLEEP_POLICY =" + wifiSleepPolicy); Settings. system. putInt (getContentResolver (), android. provider. settings. system. WIFI_SLEEP_POLICY, Settings. system. WIFI_SLEEP_POLICY_NEVER); wifiSleepPolicy = Settings. system. getInt (getContentResolver (), android. provider. settings. system. WIFI_SLEEP_POLICY, Settings. system. WIFI_SLEEP_POLICY_DEFAULT); System. out. println ("---> modified Wifi sleep policy value WIFI_SLEEP_POLICY =" + wifiSleepPolicy );}}
Main. xml is as follows: