During the development process, it is found that the cell phone lock screen/hibernate can cause communication problems. After debugging, it turns out that the socket is disconnected, which is connected to WiFi, and the WiFi problem is related to the phone's sleep.
1. can be set manually
Settings-Wireless and network--wlan--Advanced Settings-keep the WLAN open during sleep-always
However, it is not so that a mobile phone has this setting, because some systems have been customized by the developer (Lot cut).
2. Code Setup[Java] View Plain copy public void setwifidormancy (context context) { int value = settings.system.getint (Context.getcontentresolver (), Settings.system.wifi_sleep_policy, settings.system.wifi_sleep_policy_default); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D (tag, "setwifidormancy () returned: " + value); final SharedPreferences Prefs = context.getsharedpreferences ("Wifi_sleep_policy", context.mode_private); sharedpreferences.editor editor = prefs.edit (); editor.putint (Configmanager.wifi_sleep_policy, value); editor.commit (); &NBSP;&NBSP;&NBSp; if (Settings.system.wifi_sleep_policy_never != value) { settings.system.putint ( Context.getcontentresolver (), settings.system.wifi_sleep_policy, settings.system.wifi_sleep_policy_ NEVER); } } [Java]View plain copy//restore[Java]View plain copy public void Restorewifidormancy {final Sharedpreferences prefs = Context.getsha Redpreferences ("Wifi_sleep_policy", context.mode_private); int defaultpolicy = Prefs.getint (Configmanager.wifi_sleep_policy, Settings.System.WIFI_SLEEP_POLICY_DEFAULT); Settings.System.putInt (Context.getcontentresolver (), Settings.System.WIFI_SLEEP_POLICY, Defaultpolicy); Add Permission: <uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
ref:http://blog.csdn.net/mrlixirong/article/details/24938637
However, the application to me is not valid. The author finally used the service to solve "when the service is used, the network connection is maintained smoothly." And no matter how the WiFi hibernation policy is set, the black screen will stay connected after all. The next time you can verify this method.
3. On the basis of 2, combined with the use of PowerManager. [HTML] view Plain copy powermanager pm = (powermanager) getsystemservice (Context.power_service); Powermanager.wakelock wl = Pm.newwakelock (Powermanager.screen_dim_wake_lock, "my Tag"); Wl.acquire (); .. Screen'll stay on during this section. Wl.release ();
Because app uses activity, so in OnCreate Wl.acquire (), in OnDestroy wl.release (); Add Permission: Android.permission.WAKE_LOCK
This is a temporary solution to the problem.
Ref:http://stackoverflow.com/questions/22025888/keeping-wifi-connection-on-when-android-mobile-sleeps
From: http://blog.csdn.net/hhbgk/article/details/49997291