Nov 29 | 7:25 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Nov 29 | 7:30 PM |
Ron T. | has entered the room |
Mark M. |
howdy, Ron!
|
Mark M. |
how can I help you today?
|
Ron T. |
Hello Mark, thought I'd give this a try. I have a couple of questions regarding my app and Wakelocks
|
Mark M. |
fire away!
|
Ron T. |
I have a multiple activity app with a standalone
background service (non-binding). I have one activity that can be
launched from the service. that activity needs to be running if the
device is in 'sleep' mode...
|
Ron T. |
I have declared at the top of the app:
|
Ron T. |
View paste
|
Mark M. |
um, this can't be in the "top of the app"
|
Nov 29 | 7:35 PM |
Mark M. |
do you mean "top of the activity"?
|
Ron T. |
I will do the Wakelock release in the onDestroy.
This seems to work fine and eliminate the problems I had before. Is this
the proper way to use wakelocks? (OOPS, top of activity!)
|
Mark M. |
well, it's not what I'd do
|
Ron T. |
What do you recommend?
|
Mark M. |
first, for activity-level keep-the-screen-on stuff, I'd use android:keepScreenOn
|
Mark M. |
that's an attribute you can put on any widget in a layout
|
Mark M. |
so long as that widget is around, the screen stays on
|
Ron T. |
Customer does not want the screen on all the time (I tried that)
|
Mark M. |
well, either it's on or it isn't
|
Mark M. |
you said the screen should be on
|
Mark M. |
now you're saying the screen should not be on
|
Mark M. |
which is it?
|
Mark M. |
ok, wait a moment
|
Ron T. |
What they like to do is a momentary depress of the
Power button. Also, if the screen times out. The real issue is that the
activity has a timer going that must run until the user exits the
activity. If the screen times out, the timer MUST continue!
|
Mark M. |
what does it mean to you for an activity to be running without the screen being on?
|
Mark M. |
then the timer shouldn't be part of an activity
|
Mark M. |
that's what your service is for
|
Ron T. |
Here is more...
|
Ron T. |
The activity is launched by the service which
informs the activity that it needs to notify the user. The activity will
display a notification in its own view and give the user the option to
acknowledge. It starts a timer to nag the user to acknowledge. The nag
is vibrate and beep. The nag must continue if the screen is off!!
|
Nov 29 | 7:40 PM |
Mark M. |
then the "vibrate and beep" should be done by the service
|
Mark M. |
in particular, your current approach means the CPU will never turn off if the user presses HOME while your activity is on-screen
|
Ron T. |
Yeah, I thought about retro'ing it to the service but.. the activity can also be launched by a C2DM Receiver.
|
Mark M. |
then the C2DM receiver should be notifying the service, not starting the activity
|
Mark M. |
rephrasing a previous point, the device's CPU will stay on for possibly hours, until Android destroys the activity
|
Mark M. |
and, since I haven't tested leaking a WakeLock and
having my process be terminated, if Android does *not* call
onDestroy(), you might be in bigger trouble
|
Mark M. |
I assume Android does the right thing there,
otherwise lots of people are in trouble, but it's something I need to
research someday
|
Ron T. |
We have been considering an architecture change
but it has 95% of the functionality with the current evolved design. We
tested the HOME issue with the wakelock and it works fine.
|
Mark M. |
well, you asked for my opinion, so I'm giving it to you... :-)
|
Nov 29 | 7:45 PM |
Ron T. |
No Problem! I'm looking for other ideas. The
client views this as a 27/7 app and is always running. Once this
activity is on, it is the users responsiblity to acknowledge it (button
press) at which point I finish the activity.
|
Ron T. |
OOPS 24/7!
|
Mark M. |
blech
|
Mark M. |
don't much care for everlasting services
|
Mark M. |
users care for them even less
|
Ron T. |
If I move the nag to the service, won't that still use CPU?
|
Mark M. |
sure
|
Mark M. |
anything using a WakeLock keeps the CPU on
|
Mark M. |
but usually partial WakeLocks are used by background things
|
Mark M. |
like services
|
Ron T. |
The only other concern I have is that since the
activity can be launched from my service and also from a C2DM receiver,
is the Wakelock implementation I have sufficient or does there need to
be some sort of 'overlap'?
|
Mark M. |
what is causing the service to be executing code at the time when it decides to display the activity?
|
Mark M. |
(e.g., AlarmManager?)
|
Ron T. |
It has posted a REST service push request. It will
hang on the REST request until a message appears or it times out. In
the event of time out, I just re-issue the REST request.
|
Nov 29 | 7:50 PM |
Ron T. |
Essentially it is our version of C2DM!
|
Mark M. |
um, that doesn't make much sense, unless you are keeping the CPU on 24/7
|
Mark M. |
yeah, well, C2DM had to go through some pains to get that to work without keeping the CPU on all the time
|
Mark M. |
regardless
|
Mark M. |
you need to be acquiring a WakeLock *immediately* upon receiving the REST message
|
Ron T. |
PS: my Service holds a partial wakelock as well.
|
Mark M. |
um, when, and for how long?
|
Ron T. |
Now, I acquire it on service start and release it on stop.
|
Mark M. |
There is no "on stop" in a service
|
Ron T. |
it's in onDestroy
|
Mark M. |
when is the service ever being destroyed?
|
Nov 29 | 7:55 PM |
Ron T. |
it runs all the time except when the activity is
launched. The activity issues a stop while it is waiting for the user to
acknowledge the notification. When the activity finishes (user presses
button), it restarts the service.
|
Mark M. |
is this device powered by a battery?
|
Mark M. |
because you are effectively saying you are keeping
the CPU on all the time -- either the service holds a WakeLock, or the
activity does
|
Mark M. |
which means the battery life will be pathetic
|
Mark M. |
if this is a normal phone or tablet
|
Ron T. |
The attractive thing about C2DM when we started it
was that it would 'wake up...' But we can figure out if that meands
waking up the app or waking up the device.
|
Ron T. |
Both phones and tablets.
|
Mark M. |
C2DM will wake up the device and send a broadcast to your receiver
|
Mark M. |
your receiver needs to acquire a WakeLock before
doing much else, though, as there is no guarantee that the device will
stay awake
|
Mark M. |
the objective with WakeLocks is to use them for seconds, not weeks
|
Mark M. |
having a service with an always-acquired WakeLock is evil
|
Ron T. |
This led me to various Googling session regarding the definition of 'sleep' and how I found your posts...
|
Nov 29 | 8:00 PM |
Mark M. |
AFAIK, Android only has one sleep level
|
Mark M. |
certainly, that's the case in terms of the SDK
|
Mark M. |
we've been told that sleep mode on an Android device powers down the CPU
|
Ron T. |
We can disable my service and currently rely on
C2DM but when the user selects 'sleep' mode from the power menu, we do
NOT see C2DM waking up the device!
|
Mark M. |
whether that's really true, or if it goes into some ARM C-level sleep state, I can't say
|
Mark M. |
there is no "sleep mode" in the power menu of any Android device I have ever used
|
Mark M. |
which is about 20 or so
|
Ron T. |
Yeah, my biggest grip is that there is no formal
specification for 'sleep'. Either it needs to be an Android
specification or something at the Mgr level.
|
Mark M. |
um, OK
|
Ron T. |
I have a DROIDX running 2.3.3, when I long press
the power button I get a context menu with: 'Silent Mode', 'Airplane
Mode', 'Sleep', 'Power Off'.
|
Mark M. |
you'd have to ask Motorola
|
Mark M. |
I would expect that to just do the same as a simple tap on the power button
|
Mark M. |
which turns off the screen
|
Mark M. |
and eventually will lead the device to sleep mode
|
Mark M. |
during which time C2DM definitely still works
|
Ron T. |
Don't get me wrong, I would like to architect to
improve battery life. Since this is my first Android app, I'm assuming
all the difficulties on my side.
|
Nov 29 | 8:05 PM |
Ron T. |
As far as the C2DM receiver, do I need a wakelock in it?
|
Mark M. |
yes
|
Mark M. |
there is no guarantee how long you will stay awake
|
Mark M. |
the only place where there *is* a guarantee is if
you are using AlarmManager, and have the PendingIntent you schedule set
to send a broadcast
|
Mark M. |
then, and only then, Android will guarantee that the device will stay awake, long enough for the onReceive() call to complete
|
Mark M. |
C2DM, AFAIK, is not using AlarmManager for this
|
Mark M. |
(there's a presentation from the 2010 Google I|O conference that goes into details)
|
Mark M. |
so, if you're doing much of anything, I'd nab a WakeLock long enough to get that work done
|
Mark M. |
and then release the WakeLock ASAP
|
Ron T. |
Right now, both my service and my C2Dk receiver
are launching the activity with regular intents. I'm not up on the
Pending Intent use.
|
Mark M. |
you don't need PendingIntents there
|
Mark M. |
in my description above, that was related to
AlarmManager, and the one scenario where Android guarantees that your
device will stay awake
|
Mark M. |
when your C2DM receiver gets control, there is no guarantee how long the device will stay awake
|
Mark M. |
if you need a guarantee, acquire a WakeLock
|
Ron T. |
In the C2DM receiver, if I get a wakelock at the
top of the onReceive and release at the bottom of the onReceive then
would it be enough til the onCreate of the activity fires?
|
Mark M. |
no
|
Mark M. |
because startActivity() does not take immediate effect
|
Mark M. |
the activity will not even begin to start up until after onReceive() ends
|
Nov 29 | 8:10 PM |
Ron T. |
Yeah, that is what I thought. Can I define a wakelock that is public, i.e. spans those events?
|
Mark M. |
yes, carefully
|
Ron T. |
Any complete code examples out there anywhere?
|
Mark M. |
I recommend reading up on AlarmManager and
WakefulIntentService in your copy of _The Busy Coder's Guide to Advanced
Android Development_
|
Mark M. |
WakefulIntentService has an implementation of the handoff pattern, with WakeLock management
|
Mark M. |
while WakefulIntentService itself is not
necessarily relevant for your use case, understanding what it does and
why will help you apply the same sort of pattern for your scenario
|
Ron T. |
I remember seeing the WIS code there but it seemed
to be out of my skill set. I hear alot about Alarm Manager but not sure
how to use it in this case, namely we don't schedule anything.
|
Mark M. |
I feel like I'm saying the same thing several times
|
Mark M. |
you do not need AlarmManager
|
Mark M. |
I repeat: you do not need AlarmManager
|
Mark M. |
you do not need to *use* WakefulIntentService
|
Ron T. |
Just wanted to make sure (my boss and the client keep saying I do)
|
Mark M. |
it will help you understand how to solve your problem if you understand how WakefulIntentService works
|
Mark M. |
and, who knows, your boss and client may be rigth
|
Mark M. |
er, right
|
Mark M. |
nothing in what you have written here screams "AlarmManager" to me
|
Mark M. |
however, I don't know your whole app
|
Nov 29 | 8:15 PM |
Mark M. |
the C2DM-starts-an-activity scenario is just not terribly common
|
Mark M. |
if there's more than a couple dozen apps in the world that do that, I'll be surprised
|
Mark M. |
hence, you need to learn to triangulate techniques
|
Mark M. |
WakefulIntentService shows you how to have a WakeLock span components -- in that case, BroadcastReceiver to a Service
|
Mark M. |
you can, in principle, apply the same technique to your BroadcastReceiver to Activity scenario
|
Mark M. |
along the way, you'll learn about AlarmManager,
not so much that you absolutely need it, but that it's tied up in the
WakefulIntentService coverage
|
Ron T. |
The big thing is that once a notification comes in
either through my service or C2DM, my obligation is to wake up the
device/app and present a screen to the user with nagging until it is
taken care of.
|
Ron T. |
Thanks for the info! I've got enough to continue
on for now. I had thought about attending your NY class but could not
schedule it!
|
Mark M. |
there's an NYC class every two months
|
Mark M. |
DC ones should be starting up again soon as well, on the opposite months
|
Ron T. |
Also, we may still want to contract you as per my
email a couple of days ago. I do like this chat feature and I'll try not
to newbie you to death! Thanks again. over and out!
|
Mark M. |
thanks, and have a pleasant day!
|
Ron T. | has left the room |
Nov 29 | 8:30 PM |
Mark M. | turned off guest access |