I would like to run a service on background when the phone is idle (the screen is off). How can I do it? Could you give me an example or a reference to a website? Thank you.
Asked
Active
Viewed 1,476 times
2
-
Are you *sure* you need a service? Are you performing an action that needs to be run nearly constantly OR do you need to run on a schedule (1 min, 5 mins, hour, day, week, month)? – Graham Smith Mar 13 '12 at 22:21
-
@GrahamSmith i would like to run it only when the phone is idle....not on schedule – Jessy Jameson Mar 13 '12 at 22:32
-
That only partially answers the question, whatever is done while idle, does it need to be constantly. Services, while they have a place, are a battery drain. In many cases I have seen services used where they aren't needed. – Graham Smith Mar 13 '12 at 22:34
-
1http://developer.android.com/reference/android/os/PowerManager.html#isScreenOn%28%29 maybe this can help – Emran Mar 13 '12 at 22:36
-
do you have any suggestions ? – Jessy Jameson Mar 13 '12 at 22:38
-
2Look at this answer I gave previously http://stackoverflow.com/questions/8615543/running-task-periodicalyonce-a-day-once-a-week/8615600#8615600 You could turn the Alarm on/off when the phone goes between idle and not. However I have not tried accessing the devices state like this before. – Graham Smith Mar 13 '12 at 22:41
-
"i would like to run it only when the phone is idle....not on schedule" -- that is singularly horrible. What you are saying is that the phone CAN NEVER GO TO SLEEP. Battery life will be atrocious as you keep the CPU (and perhaps other stuff) on 24x7. – CommonsWare Mar 13 '12 at 23:21