Set the Android device to always enable WLAN during sleep

Source: Internet
Author: User

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:

     
  
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.