Android Programming Tutorials

(Romina) #1

TUTORIAL 29


Android Would Like Your


Attention


From time to time, Android can tell you things that may be of interest, such


as when the battery gets low. This allows you to take action based on those


system events, such as reducing the amount of background work you do


while the battery is low. In this tutorial, we will update the PostMonitor


service to be gentler while the battery is low, by polling less frequently.


Step-By-Step Instructions........................................................................


First, you need to have completed the previous Patchy tutorial. If you are


beginning the tutorials here, or if you wish to not use your existing work,


you can download a ZIP file with all of the tutorial results, and you can copy


the 28-UsingCP edition of Patchy to use as a starting point.


Step #1: Track the Battery State


We need to register a BroadcastListener from PostMonitor to be informed of


changes in state in the battery.


First, add the registerReceiver() call to onCreate() in PostMonitor:


@Override
public void onCreate() {
super.onCreate();

285
Free download pdf