Login     Sign Up
Cyril Sermon (@admin)
10 months ago
57 Views

Depending on the device, there may be several technologies that Android can use to determine the cur-rent location. Each technology, or Location Provider, will offer different capabilities including power con-sumption, monetary cost, accuracy, and the ability to determine altitude, speed, or heading information.

To get an instance of a specific provider, call getProvider, passing in the name:

String providerName = LocationManager.GPS_PROVIDER; LocationProvider gpsProvider;

gpsProvider = locationManager.getProvider(providerName);

This is generally only useful for determining the abilities of a particular provider. Most Location Manager methods require only a provider name to perform location-based services.

Finding the Available Providers

The LocationManager class includes static string constants that return the provider name for the two most common Location Providers:

❑LocationManager.GPS_PROVIDER

❑LocationManager.NETWORK_PROVIDER

To get a list of names for all the providers available on the device, call getProviders, using a Boolean to indicate if you want all, or only the enabled, providers to be returned:

boolean enabledOnly = true;

List<String> providers = locationManager.getProviders(enabledOnly);

Finding Providers Based on Requirement Criteria

In most scenarios, it’s unlikely that you will want to explicitly choose the Location Provider to use. More commonly, you’ll specify the requirements that a provider must meet and let Android determine the best technology to use.

Use the Criteria class to dictate the requirements of a provider in terms of accuracy (fine or coarse), power use (low, medium, high), cost, and the ability to return values for altitude, speed, and bearing.

The following code creates Criteria that require coarse accuracy, low power consumption, and no need for altitude, bearing, or speed. The provider is permitted to have an associated cost.

Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setSpeedRequired(false); criteria.setCostAllowed(true);

Having defined the required Criteria, you can use getBestProvider to return the best matching Location Provider or getProviders to return all the possible matches. The following snippet demon-strates using getBestProvider to return the best provider for your criteria where the Boolean lets you restrict the result to a currently enabled provider:

String bestProvider = locationManager.getBestProvider(criteria, true);

If more than one Location Provider matches your criteria, the one with the greatest accuracy is returned. If no Location Providers meet your requirements, the criteria are loosened, in the following order, until a provider is found:

  • ❑ Power use
  • ❑ Accuracy
  • ❑Ability to return bearing, speed, and altitude

The criterion for allowing a device with monetary cost is never implicitly relaxed. If no provider is found, null is returned.

To see a list of names for all the providers that match your criteria, you can use getProviders. It accepts Criteria and returns a filtered String list of all available Location Providers that match them. As with the getBestProvider call, if no matching providers are found, this call returns null.

List<String> matchingProviders = locationManager.getProviders(criteria, false);