The scenario is that I am writing a small app that I want to run in
the background only when the device is charging. My problem is that
this appears to impossible to achieve.

The documentation helpfully informs me that ACTION_BATTERY_CHANGED
intents cannot be received through components declared in manifests
(Why this is this? I'm interested), instead Context.registerReceiver()
must be used. But this method cannot be called from a
BroadcastReceiver - again the documentation is clear on this point.

I am assuming that any dynamically registered BroadcastReceiver is
bound to the lifetime of the Component that registered it (Is this
correct? The documentation appears to be silent on this point). And
since I don't want my application running unless the device is being
powered, I don't have a suitably long-lived component to register it
under. The result is that I can't seem to obtain battery state
notifications in any way that is useful for starting the work of my
application.

My next thought was to poll: use the AlarmManager to periodically run
the BroadcastReceiver, poll the state of the battery, and only do the
work when the battery is in the desired state. But (and this was a
surprise) there doesn't appear to be any method in the API for polling
the battery state. So I can't see that approach working either.

Now it's seems quite possible to me that I'm missing something very
simple and obvious which can resolve this (please put me out of my
misery), but apart from obvious half-arsed approaches that run a
service in the background to monitor the battery state - a total waste
of resources for something that is intended to run just a couple of
minutes a day - I just can't see any way of doing this.

Is it really the case that it's impossible to start an application in
response to a change in battery state?

Tom.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to