Problem with Android hibernate socket disconnected

Source: Internet
Author: User

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

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.