I am thinking about having two separate alarms to gather a user's location data every hour, one that goes off every 59 minutes to "connect" the client and a second to actually get the location and then subsequently disconnect the client.
In terms of battery life, is there anything else I should consider doing if getting the user's location will be the primary drain of the app? Or, is there a different approach to having two alarms? I originally only had a single alarm, but performing a (!mLocationClient.isConnected) then connect check does not give the client enough time to connect.
Thanks for your insight.
The two alarms would go off something like this:
private int PERIODIC_UPDATE = 60000*60; //gets location and disconnects every hour
private int PERIODIC_RECONNECTION_UPDATE = 60000*59; //connects 1 minute before getLocation call
Timer toReconnect = new Timer();
toReconnect.schedule(new TimerTask() {
@Override
public void run() {
mLocationClient.connect();
}
}, 5000, PERIODIC_RECONNECTION_UPDATE);
Timer theTimer = new Timer();
theTimer.schedule(new TimerTask(){
@Override
public void run() {
try {
if(!mLocationClient.isConnected()) {
mLocationClient.connect();
//This will not have much affect because cannot so quickly, will remove.
}
Location theLocation = mLocationClient.getLastLocation();
if(theLocation!=null) {
checkPostLocation(theLocation);
mLocationClient.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}}, 5000, PERIODIC_UPDATE);