The
Mainactivity is as follows:
Package Cc.ab;
Import Android.os.Bundle;
Import android.provider.Settings;
Import android.app.Activity;
/** * Demo Description: * Set the device to always keep the WLAN open 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 very much * Note: * 1 This is used and
Roid.provider.Settings.System.WIFI_SLEEP_POLICY * and not Settings.System.WIFI_SLEEP_POLICY!!!!!!!!!!!!!!!!!!!!!!! * * 2 permissions <uses-permission android:name= "Android.permission.WRITE_SETTINGS"/> * * 3 in the actual project, this is so mandatory to set up WiFi in hibernation never disconnect
A little rude.
* You can record the original WiFi strategy, after our logic is complete and then restore back to the good. * * Public class Mainactivity extends activity {@Override protected 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_PO
Licy_default);
SYSTEM.OUT.PRINTLN ("---> Pre-Modified WIFI hibernation policy value wifi_sleep_policy=" +wifisleeppolicy); Settings.System.putInt (Getcontentresolver (), Android.provider.Settings.System.WIFI_SLEEP_POLICY, S Ettings.
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 Hibernate policy value wifi_sleep_policy=" +wifisleeppolicy); } <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android xmlns:tools=" http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "> < TextView android:layout_width= "wrap_content" android:layout_height= wrap_content "android:text=" @string/hello_ World "/&Gt
</RelativeLayout>
The
Code is as above, but in the actual project, it is rude to set up a wifi that is never disconnected during hibernation.
You can record the original WiFi policy, and then revert back after our logic is complete.