问题
Posts from over a year ago suggest that it is necessary to use a WakeLock to reserve CPU to gather location information (CommonsWare in particular here Android Periodic GPS location updates with AlarmManager inside a Service). However I do not understand why I cannot simply call a Service every 28 minutes, connect the LocationClient, set a timer to wait 2 minutes, then get the most recent location, disconnect the client, and then stop the service.
Because Commonsware and the user who forked his example here (https://github.com/alexbirkett/cwac-locpoll/blob/master/src/com/commonsware/cwac/locpoll/LocationPollerService.java) use a LocationListener, and not a LocationClient, I was wondering whether his answer still aplies.
Thanks for your time!
EDIT: Is it this simple, where an AlarmManager calls a Service every 28 minutes > the Service acquires a WakeLock and a timer elapses for two minutes as the LocationClient is connect > once two minutes has elapsed, I can kill the service and release the WakeLock?
It would look something like this:
PowerManager pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
Timer theTimer = new Timer();
theTimer.schedule(new TimerTask() {
@Override
public void run() {
if(checkIfGooglePlay()) {
getPostLocation();
w1.release();
stopSelf();
}
}
}, TWO_MINUTES);
return Service.START_NOT_STICKY;
回答1:
However I do not understand why I cannot simply call a Service every 28 minutes, connect the LocationClient, set a timer to wait 2 minutes, then get the most recent location, disconnect the client, and then stop the service.
You probably can poll the LocationClient
. However, you will still need a WakeLock
, as the device will fall asleep during those two minutes otherwise, you will still need AlarmManager
for the "every 28 minutes" part, and I don't know if LocationClient
will impose other requirements beyond those.
来源:https://stackoverflow.com/questions/17368540/do-i-need-to-use-an-alarmmanager-and-wakelock-to-periodically-use-a-locationclie