Learning Android™ Application Programming: A Hands-On Guide to Building Android Applications
by James Talbot, Justin McLean, Jorge Hernandez
Checking the Battery Regularly
Checking the battery once is not very useful. It would be far more useful to check the battery level at regular intervals and then perform the correct action. Don’t forget that the battery can be charging, so the battery level can also go up.
Constantly checking the battery power itself drains the battery, so it is better to check it every 15 or 30 minutes. You can use the AlarmManager to provide this functionality.
1. Create a new package named com.androiddevbook.onyourbike.chapter9.receivers. Inside this package, create a new class named BatteryCheck that extends BroadcastReciever. Create two constructors—one that takes a context, and one that takes no parameters—and then set up the class name for logging in both. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access