THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this tutorial is to help you to get location information.

 

You will get Final Output:

Location Info

 

Step 1 Get Location

To get the location manager, we ask the context to getSystemService() for location services. Next we ask the location manager for list of all available providers. You may not care about all available location providers but just want to get the best one available. To do this, we specify our criteria for best.

For example, do we require altitude and bearing, allow cost, etc. For this we create new method named getLocations().

private void getLocations() {
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
 
// List all providers:
List<String> providers = locationManager.getAllProviders();
for (String provider : providers) {
printProvider(provider);
}
 
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
output.append("\n\nBEST Provider:\n");
printProvider(bestProvider);
 
output.append("\n\nLocations (starting with last known):");
Location location = locationManager.getLastKnownLocation(bestProvider);
printLocation(location);

For more about LocationManager class please refer:

 

Step 2 Manage States of Location

Now we add different methods for the different states of the Locations.

private void printProvider(String provider) {
LocationProvider info = locationManager.getProvider(provider);
output.append(info.toString() + "\n\n");
}
private void printLocation(Location location) {
if (location == null)
output.append("\nLocation[unknown]\n\n");
else
output.append("\n\n" + location.toString());
}
public void onStatusChanged(String provider, int status, Bundle extras) {
output.append("\n\nProvider Status Changed: " + provider + ", Status="
+ S[status] + ", Extras=" + extras);
}

For more details about LocationProvider class please refer:

I hope you enjoy this tutorial and it would be helpful to you.

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

An entrepreneur who has founded 2 flourishing software firms in 7 years, Tejas is keen to understand everything about gaming - from the business dynamics to awesome designs to gamer psychology. As the founder-CEO of a company that has released some very successful games, he knows a thing or two about gaming. He shares his knowledge through blogs and talks that he gets invited to.