give-up screen (going to sleep)

147 views
Skip to first unread message

Dex Wood

unread,
Oct 23, 2008, 4:21:10 PM10/23/08
to Android Developers
I am developing an application that uses the accelerometer and/or gps
inside of a service. It will be in use while the service runs in the
background until the user terminates the service. I am using usb
debugging on the device and I noticed in logcat it is saying give-up
screen and my service stops getting input from the accelerometer. Is
there a way to have a service run while the phone is idle for a period
of time without having to disable the key-lock?

Justin (Google Employee)

unread,
Oct 23, 2008, 4:26:55 PM10/23/08
to Android Developers
When the screen goes off, the device goes to sleep. This means that
the main CPU shuts off. You can prevent this by holding a wakelock.
WARNING: Holding a wake lock for a substantial period of time will
have a terrible effect on battery life. Android achieves better
battery life by doing as little as possible and sleeping as much as
possible. Please consider the best interests of your users when doing
things that will adversely effect their experience.

Cheers,
Justin
Android Team @ Google

Dex Wood

unread,
Oct 23, 2008, 5:56:45 PM10/23/08
to Android Developers
I have observed that when using the music application, the music
continues to play when the device appears to go to sleep. Does this
application use a wake lock?

Thank you for your reply

On Oct 23, 3:26 pm, "Justin (Google Employee)" <j...@google.com>
wrote:

nkijak

unread,
Oct 24, 2008, 8:22:53 AM10/24/08
to Android Developers
Without looking at the source I would say probably with
"PowerManager.PARTIAL_WAKE_LOCK". Just a guess though.

Justin (Google Employee)

unread,
Oct 24, 2008, 12:08:00 PM10/24/08
to Android Developers
> Does this
> application use a wake lock?

By definition, yes. A wake lock is the only way to keep the system
awake.

Cheers,
Justin
Android Team @ Google

blindfold

unread,
Oct 26, 2008, 2:26:43 PM10/26/08
to Android Developers
Is a PARTIAL_WAKE_LOCK sufficient to keep CPU, camera, audio and
sensors awake? This is what my app requires. (I do not care too much
about the screen brightness and keyboard backlight.)

Thanks

On Oct 24, 5:08 pm, "Justin (Google Employee)" <j...@google.com>
wrote:

Brian

unread,
Oct 28, 2008, 1:38:39 AM10/28/08
to Android Developers
I am very confused..if the CPU shuts off when the device goes to sleep
does that mean that any thread running will also be shut down?

I am confused..how can one have a process run in the background if the
CPU is off most of the time?

hackbod

unread,
Oct 28, 2008, 2:17:44 AM10/28/08
to Android Developers
On Oct 27, 10:38 pm, Brian <send2world....@gmail.com> wrote:
> I am very confused..if the CPU shuts off when the device goes to sleep
> does that mean that any thread running will also be shut down?

Well, it will stop executing.

> I am confused..how can one have a process run in the background if the
> CPU is off most of the time?

You don't, as much as possible. Keeping the CPU running while it's
not being used will have a big impact on battery life. You should use
the alarm manager to have the system wake you up when needed. You can
hold a partial wake lock while needed to ensure that the cpu remains
running during that time, but just holding one indefinitely is evil.

Brian

unread,
Oct 28, 2008, 10:15:28 AM10/28/08
to Android Developers
I see...

I guess more specifically.. if I use

Thread.sleep(1000000);

will the thread wake up 1000 seconds later if the phone is asleep?
or must I use the AlarmManager to do something like this?

Jean-Baptiste Queru

unread,
Oct 28, 2008, 10:21:51 AM10/28/08
to android-d...@googlegroups.com
This describes the various clocks and their behaviors:

http://code.google.com/android/reference/android/os/SystemClock.html

AlarmManager is what you're looking for.

JBQ

hackbod

unread,
Oct 28, 2008, 1:26:53 PM10/28/08
to Android Developers
No, there is no guarantee about that. As I said, use the Alarm
Manager.

Plus it is big waste of resources to have a thread sitting there doing
nothing. If you want to want for minutes or more of time, schedule an
alarm to wake you up (possibly relaunching your app if it needed to be
killed for resources elsewhere) at the desired time.
Reply all
Reply to author
Forward
0 new messages