On Wed, Feb 2, 2011 at 9:57 PM, Jeffrey <[email protected]> wrote:
> I've got a battery widget I'm working on and I have a broadcast
> receiver for when the battery status changes, but I don't know where
> it needs to go. I tried putting it in the Configuration activity, but
> I keep getting this error:
>
> Activity com.android.blah has leaked IntentReceiver com.android.blah
> $2@43758c58 that was originally registered here. Are you missing a
> call to unregisterReceiver()?
>
> Please let me know what I'm doing wrong, I'm pretty new to broadcast
> receivers, so if I'm missing something basic please let me know.

I wouldn't use a broadcast receiver in this case. That would require
you to keep a service running 24x7, to host the receiver, and that is
bad for business.

Rather, just check the battery level periodically:

 - via updatePeriodMillis in your metadata, or
 - via AlarmManager, if the 30-minute minimum for updatePeriodMillis
will be too long (probably would be, in this case)

To check the battery level, call registerReceiver() with the
ACTION_BATTERY_CHANGED IntentFilter, but a null BroadcastReceiver. The
Intent that is returned to you will be the last-broadcast battery
change Intent, from which you can get the last-known battery level.

You can even let the user configure the polling period, to balance
between accuracy and battery consumption by the battery app widget.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://bit.ly/smand1 and http://bit.ly/smand2

-- 
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