Login     Sign Up
Tugadar (@admin)
16 days ago

The purpose of location-based services is to find the physical location of the device.

Access to the location-based services is handled using the Location Manager system Service. To access the Location Manager, request an instance of the LOCATION_SERVICE using the getSystemService method, as shown in the following snippet:

String serviceString = Context.LOCATION_SERVICE; LocationManager locationManager;
locationManager = (LocationManager)getSystemService(serviceString);

Before you can use the Location Manager, you need to add one or more uses-permission tags to your manifest to support access to the LBS hardware.

The following snippet shows the fine and coarse permissions. Of the default providers, the GPS provider requires fine permission, while the Network provider requires only coarse. An application that has been granted fine permission will have coarse permission granted implicitly.

<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/> <uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>

You can find the last location fix determined by a particular Location Provider using the getLastKnownLocation method, passing in the name of the Location Provider. The following example finds the last location fix taken by the GPS provider:

String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);

Note that getLastKnownLocation does not ask the Location Provider to update the current posi-tion. If the device has not recently updated the current position this value may be be out of date.

The Location object returned includes all the position information available from the provider that sup-plied it. This can include latitude, longitude, bearing, altitude, speed, and the time the location fix was taken. All these properties are available using get methods on the Location object. In some instances, additional details will be included in the extras Bundle.

Where Am I?” Example

The following example — “Where Am I?” — features a new Activity that finds the device’s current location using the GPS Location Provider. You will expand on this example throughout the chapter as you learn new geographic functionality.

This example assumes that you have enabled the GPS_PROVIDER Location Provider using the tech-niques shown previously in this chapter, or that you’re running it on a device that supports GPS and has that hardware enabled.

Create a new WhereAmI project with a WhereAmI Activity. This example uses the GPS pro-vider (either mock or real), so modify the manifest file to include the uses-permission tag for ACCESS_FINE_LOCATION.

<?xml version=”1.0” encoding=”utf-8”?>

<manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.paad.whereami”>







<action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” />




<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>


Modify the main.xml layout resource to include an android:ID attribute for the TextView con-trol so that you can access it from within the Activity.

<?xml version=”1.0” encoding=”utf-8”?>












Override the onCreate method of the WhereAmI Activity to get a reference to the Location Manager. Call getLastKnownLocation to get the last location fix value, and pass it in to the updateWithNewLocation method stub.

package com.paad.whereami;

import android.app.Activity;

import android.content.Context;

import android.location.Location;

import android.location.LocationManager;

import android.os.Bundle;

import android.widget.TextView;

public class WhereAmI extends Activity {


public void onCreate(Bundle icicle) {



LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(context);

String provider = LocationManager.GPS_PROVIDER;

Location location = locationManager.getLastKnownLocation(provider);



private void updateWithNewLocation(Location location) { }


Fill in the updateWithNewLocation method to display the passed-in Location in the Text View by extracting the latitude and longitude values.

private void updateWithNewLocation(Location location) { String latLongString;

TextView myLocationText;

myLocationText = (TextView)findViewById(R.id.myLocationText);

if (location != null) {

double lat = location.getLatitude();

double lng = location.getLongitude();

latLongString = “Lat:” + lat + “\nLong:” + lng;

} else {

latLongString = “No location found”;


myLocationText.setText(“Your Current Position is:\n” + latLongString);


When running, your Activity should look like Figure 7-3.