6

I am doing one application for that I need to put a android device to sleep mode. How to put a device to sleep mode and how to wake-up a device when it is in sleep mode.

Thanks in advance

shiv1229
  • 357
  • 2
  • 6
  • 19

1 Answers1

1

How to put a device to sleep mode

You don't. You allow the device to fall asleep of its own accord, when the user is done working with it, based upon the user's preferences as set up in Settings.

how to wake-up a device when it is in sleep mode.

If it is supposed to wake up at a certain time, use AlarmManager with a getBroadcast() PendingIntent. The device will stay awake long enough for your BroadcastReceiver to be called with onReceive(). Once onReceive() returns, the device can fall back asleep. If your work is too long to do in onReceive(), you will need to delegate that work to some other component (e.g., an IntentService) and use a WakeLock to keep the device awake long enough for you to complete the work and no longer.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • What about [`PowerManager.goToSleep(long time)`](http://developer.android.com/reference/android/os/PowerManager.html#goToSleep(long))? – Reed Jan 16 '12 at 16:23
  • @Jakar: That's protected by a permission (`DEVICE_POWER`) that normal SDK applications cannot hold. Unfortunately, this requirement is not mentioned in the JavaDocs. See http://stackoverflow.com/questions/5710971/android-what-permissions-required-to-call-powermanager-gotosleepn-put-device-i – CommonsWare Jan 16 '12 at 16:25
  • I thought that might have been the case. Thanks for clarifying. – Reed Jan 16 '12 at 19:20