Android Would Like Your Attention
You will need to add imports for android.os.BatteryManager,
android.content.Context, and android.content.BroadcastReceiver.
The isBatteryLow object is actually an AtomicBoolean, which you should add
to the data members for PostMonitor:
private AtomicBoolean isBatteryLow=new AtomicBoolean(false);
At this point, we are watching for battery events and setting isBatteryLow,
but we are not making use of that information anywhere.
Step #2: Run the Monkey...................................................................
Then, all we need to do is look at isBatteryLow in our polling loop. Modify
threadBody in PostMonitor to look like this:
private Runnable threadBody=new Runnable() {
public void run() {
while (active.get()) {
for (Account l : accounts.values()) {
poll(l);
}
int pollPeriod=POLL_PERIOD;
if (isBatteryLow.get()) {
pollPeriod*= 10 ;
}
SystemClock.sleep(pollPeriod);
}
}
};
All we do is multiply our normal polling period by a factor of 10 when the
battery is low.
To test this with an emulator, you will need to tell the emulator what charge
level to simulate. To do this: