MainActivity is as follows:
package ; import ; import ; import ; /** * Demo description: * Set the device to always keep WLAN on during sleep. * * References: * 1 /questions/8652031/how-to-modify-wi-fi-sleep-policy-programmatically/8655070#8655070 * 2 /mrlixirong/article/details/24938637 * Thank you very much * * Notes: * 1.WIFI_SLEEP_POLICY is used here * Instead of .WIFI_SLEEP_POLICY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * * 2 Permission <uses-permission android:name=".WRITE_SETTINGS"/> * * 3 In actual projects, it is a bit rude to set up WIFI so compulsively to keep on opening when sleeping. * You can first record the original WIFI strategy and restore it after our logic is completed. */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); setWifiNeverSleep(); } private void setWifiNeverSleep(){ int wifiSleepPolicy=0; wifiSleepPolicy=(getContentResolver(), .WIFI_SLEEP_POLICY, .WIFI_SLEEP_POLICY_DEFAULT); ("---> Before modificationWifiSleep policy value WIFI_SLEEP_POLICY="+wifiSleepPolicy); (getContentResolver(), .WIFI_SLEEP_POLICY, .WIFI_SLEEP_POLICY_NEVER); wifiSleepPolicy=(getContentResolver(), .WIFI_SLEEP_POLICY, .WIFI_SLEEP_POLICY_DEFAULT); ("---> ModifiedWifiSleep policy value WIFI_SLEEP_POLICY="+wifiSleepPolicy); } } <RelativeLayout xmlns:andro xmlns:tools="/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" /> </RelativeLayout>
The code is as above, but in actual projects, it is a bit rude to set up WIFI so compulsoryly to keep opening when sleeping.
You can first record the original WIFI strategy and restore it after our logic is completed.