So you want to continue to get sensor events in your
Android service, even after the screen turns off? Well
that’s too damn bad, kid, cause you can’t. No,
I’m just kidding.

But you’ll probably need a work-around for Android
Issue 3708. In this document I describe what I had to
do to get it working on my platform. My assumption is
you’ve already been Googling around, and so have a little
background on this, but are still scratching your head as
to why it’s not working. Hopefully, you won’t be in that
position after reading this!

Re-register Your Listeners When The Screen Goes Off

Some then propose to take action when the screen turns
off. In this post on
StackOverflow, for example, the author proposes
un-registering and re-registering to receive sensor
events at that time.

Use a Delayed Thread, for the Above

The above two techniques are ultimately what I’ve used to
solve the problem, except I had to make one additional
modification. When the BroadcastReceiver
receives an ACTION_SCREEN_OFF Intent, I
queue a thread to run 500ms in the future, instead of
running immediately. This ensures that most other
activities surrounding the screen off will have
completed. So at that time, 500ms later, we un-register
and re-register our service as a sensor event listener.

You may have to play with the value of
SCREEN_OFF_RECEIVER_DELAY — for me, 500
(milliseconds) was the minimal value that would get it
working.

See the Code In An Example App

I’ve posted an example app which includes the above code.
It shows a foreground service holding a
PARTIAL_WAKE_LOCK, doing the re-registration
business mentioned above. The result is a service which
continues to receive sensor events even after the screen
goes off (for whatever reason.)

I must admit that after spending DAYS searching for a
solution to this issue, all hope appeared to be lost.
That is until I saw this. Thank you so much for
taking the time to try to help others like me. You’ve
made my day!

my apologies, still very much a noob at this. the
link
“https://github.com/jamesonwilliams/AndroidPersistentSensors”
has a zip file download link. is this zip installable
like a rom or software via custom recovery is what i
meant to ask. again i apologize for the dumb
question, just want this fix bad and not sure how to
do it.

That is a link to source code for an Android
application, which demonstrates the method described
in this blog post. You would have to compile the code
into an application in order to see it working. But
the code is meant only to be demonstrative — in and
of itself, it’s not particularly useful.

Hello.
Congratulation, it’s a very interesting and helpful
post.
Just a question: is it possible to implement the
broadcast receiver in a class different from the
sensor event listener class? What I mean is, how
could I deregister/register sensor event listeners if
the broadcast receiver is not an inner class? I need
to know that because in my application I want to have
different services running in background, and they
all implement the sensor event listener interface, so
I don’t want to replicate the broadcast receiver code
in all these classes. How could I do? Thank you

Hi,
I want to switch on my device using shake. I tried to
follow your comments but i am getting following
error-( Bad notification for startForeground:
java.lang.RuntimeException: icon must be non-zero).’