Set Android device WiFi never disconnects during hibernation _android

Source: Internet
Author: User

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.

Related Article

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.