How to modify the criteria part and get location by GPS automatically in Android?
I would like to set that the device prefer to choose to get location by GPS rather than network provider, but the device still always using network provider to get location. How can I modify the criteria part and get location by GPS automatically?
The criteria I am using:
public void getLocationProvider()
{
try
{
Criteria mCriteria01 = new Criteria();
mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
mCriteria01.setAltitudeRequired(false);开发者_如何学运维
mCriteria01.setBearingRequired(false);
mCriteria01.setCostAllowed(true);
mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
strLocationProvider =
mLocationManager01.getBestProvider(mCriteria01, true);
mLocation01 = mLocationManager01.getLastKnownLocation
(strLocationProvider);
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
Instead of using the criteria to getBestProvider()
and hope that it will be a GPS-based service, you can use the criteria to return all matching providers, iterate through them looking for a GPS-based compatible service, and use the one you find:
First, grab all matching providers for your criteria:
List<String> allMatchingProviders =
mLocationManager01.getProviders(mCriteria01, true);
Then iterate through the list and test each provider for satellite-based service:
foreach provider (allMatchingProviders)
{
...
if( provider->requiresSatellite() == true )
strLocationProvider = provider
...
}
精彩评论