开发者

Wake Lock in Samsung Galaxy Tab

开发者 https://www.devze.com 2023-03-07 17:57 出处:网络
I am developing an app in which i need to send and receive packets in background. But I\'ve discovered that when screen shut off also cpu and networking is disabled. So I\'ve found on Android Develope

I am developing an app in which i need to send and receive packets in background. But I've discovered that when screen shut off also cpu and networking is disabled. So I've found on Android Developers that I should use wake lock and wifi lock to let the cpu works altought if the screen went off. This solution works for other devices such LG Optimus One but not for Samsung Galaxy Tab. With this device when screen shut off I can't receive no more packets (UDP) and magically when I press po开发者_Python百科wer button for re enabling screen, I restart to receive packets. In my code I call:

powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_W AKE_LOCK, "TAG");

then

wakeLock.acquire();

I think that is a Samsung bug but I'm not sure of this, and if someone can give me some hint i would really appreciate.

P.S. I need some programming help... I already know that if I modify some manual setting to avoid shutting down the screen, this will work. But I'm developing an app and i need code!!


You should use PARTIAL_WAKE_LOCK in your case.

I'm not sure why SCREEN_DIM_WAKE_LOCK is not enough (didn't find any documentation that justifies this), but PARTIAL_WAKE_LOCK worked just fine for me in a similar scenario.

As another option you may use WifiLock. But you need to be smart about that, as your network connection may be 2G or 3G, not Wi-Fi only. So you shouldn't hold Wi-Fi lock when you are actually using 3G/2G/etc.


You might also want to check the advanced WIFI settings. In Preferences - WIFI settings, you can hit the menu key, click the advanced icon, and check the value for WIFI Sleep policy. One of the options is "When screen turns off".

0

精彩评论

暂无评论...
验证码 换一张
取 消