7

I want to prevent Android Mobile from going into sleep mode when my threads are sending HTTP request. as what happened while threads are doing HTTP calls mobiles goes to sleep mode and when user wakes up the phone threads never complete.

User has to restart the app. what to do? please help

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
AZ_
  • 21,688
  • 25
  • 143
  • 191
  • they won't work http://www.netmite.com/android/mydroid/development/pdk/docs/power_management.html#androidPowerWakeLocks – AZ_ Mar 01 '11 at 14:21
  • 2
    thousands of apps use Wake locks so if yours does not it implies that either your phone or your app is not functioning correctly – Andrew White Mar 01 '11 at 14:28

3 Answers3

10

You need a WakeLock. There are different kinds of wake locks so be sure to select the least aggressive one that meets your needs. In particular it sounds like you need a Partial Wake Lock.

Partial Wake Lock - Wake lock that ensures that the CPU is running. The screen might not be on.

Also, make sure you add the permission android.permission.WAKE_LOCK to your manifest. And finally be double sure to Release your lock when you are done.

Andrew White
  • 52,720
  • 19
  • 113
  • 137
  • Badumtish. He probably didn't include the permission or something. – Codemonkey Mar 01 '11 at 14:24
  • @Andrew: so whats the way out? – AZ_ Mar 01 '11 at 14:24
  • @Badumtish: it will throw Exception if you don't include proper permissions. – AZ_ Mar 01 '11 at 14:25
  • @Aizaz: maybe you should file a bug? Generally, if you want to keep the screen on while the UI is displaying, you use FLAG_KEEP_SCREEN_ON for Activity window and hold a PARTIAL_WAKE_LOCK to keep the device running while you are not in its UI. – Samuh Mar 01 '11 at 15:11
4

To prevent the phone from sleeping you can use a WakeLock but you should be careful when doing this to not kill the user's battery. If the phone goes to sleep does the user really care if your app finishes the requests it was making?

You will also need the WAKE_LOCK permission.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
2

for JAVA: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
for KOTLIN: window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Use this code, it will keep your device wake up and you didn't need any permission.

VKAR
  • 21
  • 5