According to this article (UPD: link removed as it leads to some crap now), setting the minTime when requesting location updates will cause the provider to set itself to TEMPORARILY_UNAVAILABLE for minTime milliseconds in order to conserve battery power. During this period of inactivity, the GPS provider will turn itself off and the GPS icon will disappear.
In my code, I set the minTime to about 30 seconds, but the provider only becomes TEMPORARILY_UNAVAILABLE once every five minutes. When it does, it only stays TEMPORARILY_UNAVAILABLE for ten seconds at most before turning itself back on. I know this because the GPS icon disappears for only ten seconds before reappearing again.
I understand that the minTime setting is only a rough guideline for the Android location provider...but I'm pretty sure that five minutes is entirely different from 30 seconds. Does anyone know what is going on here? How does minTime and requestLocationUpdates actually work?
LocationManager Setup:
locManager = (LocationManager) getSystemService(LOCATION_SERVICE); locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,30000L, 0, locListener);
locListener:
public void onLocationChanged(Location loc) {
//Keep track of best location
//Having a location > no location
if (bestLocation == null)
bestLocation = loc;
//More accuracy > Less accuracy
else if (loc.getAccuracy() <= bestLocation.getAccuracy())
bestLocation = loc;
Log.d(TAG, "Location Updated";
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "New s开发者_如何学运维tatus: " + status);
if (status== LocationProvider.TEMPORARILY_UNAVAILABLE)
//Do stuff since the provider is temporarily off
}
Debug output on a real Android device (HTC Incredible 2.2):
Location Updated
Location Updated
New status: 2
Location Updated
Location Updated
Location Updated
New status: 2
... (five minutes later)
New status: 1
From the API:
"Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended." (emphasis is mine)
"This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value." (emphasis is mine).
If you need to know the location immediately, use getLastKnownLocation(String)
精彩评论