GPS Example

This example shows how to access information from a device's GPS receiver in order to report its status and location.

A screenshot of the application.

We import the classes and modules that will be needed by the application. The most relevant are the classes from the android.location module.

from java.lang import String
from import Activity
from android.content import Context
from android.location import GpsStatus, Location, LocationListener, \
from android.os import Bundle
from android.view import View, ViewGroup
from android.widget import ImageView, LinearLayout, ScrollView, TextView

We define a class based on the standard Activity class. This represents the application, and will be used to present a graphical interface to the user.

class GPSActivity(Activity):

    __interfaces__ = [GpsStatus.Listener, LocationListener]

The activity implements two interfaces: one which handles changes to the GPS status and another which handles general notifications about location.

The initialisation method simply calls the corresponding method in the base class and initialises two variables that are used to keep a textual log of events and record the old connection status.

    def __init__(self):
        self.log = ""
        self.oldStatus = 0

The onCreate method calls the corresponding base class method before obtaining an instance of the location service manager and setting up the user interface.

    def onCreate(self, bundle):
        Activity.onCreate(self, bundle)

We obtain a location service manager object and cast it to the correct type so that we can call its addGpsStatusListener method to register the activity as a listener. This requires us to implement the GpsStatus.Listener interface that we declared earlier.

        self.manager = CAST(self.getSystemService(Context.LOCATION_SERVICE), LocationManager)

We create a user interface that contains TextViews showing the currently known location, the number of satellites visible to the device and a log of events.

        self.locationView = TextView(self)
        self.locationView.setText("No location")
        self.satellites = TextView(self)
        self.satellites.setText("Satellites: 0")
        self.statusView = TextView(self)
        scrollView = ScrollView(self)
        layout = LinearLayout(self)

The onResume method is called when the activity is first started, or when the user navigates to it. We call the corresponding base class method and request notification of location updates every 500 milliseconds and if the device moves more than 1 metre.

    def onResume(self):
            long(500), float(1), self)

The onPause method is called when the user navigates away from the activity. We call the corresponding base class method and remove our request for location updates.

    def onPause(self):

The following four methods are part of the LocationListener interface.

We implement the onLocationChanged method to handle location changes, using the Location object supplied to update the location text displayed in the user interface.

    def onLocationChanged(self, location):
            "Latitude: " + location.convert(location.getLatitude(), Location.FORMAT_SECONDS) + \
            " Longitude: " + \
            location.convert(location.getLongitude(), Location.FORMAT_SECONDS)

In the onProviderDisabled method, we simply report that a location provider has been disabled, updating the status TextView.

    def onProviderDisabled(self, provider):
        self.addText(provider + " disabled")

Similarly, in the onProviderEnabled method, we report that a location provider has been enabled, updating the status TextView, and request updates of half a second in frequency and for changes of 1 metre or more.

    def onProviderEnabled(self, provider):
        self.addText(provider + " enabled")
            long(500), float(1), self)

The onStatusChanged method is called to inform the activity about changes to a location provider. In this example, we only report that something about the provider changed.

    def onStatusChanged(self, provider, status, extras):
        self.addText(provider + " changed")

The onGpsStatusChanged method is required by an implementation of the GpsStatus.Listener interface, declared earlier.

We only care about changes to the status, only reporting any changes if the newly delivered event differs from the old one.

    def onGpsStatusChanged(self, event):
        if event != self.oldStatus:
            self.oldStatus = event
            self.addText("GPS Status: " + str(event))

Additionally, if the event indicated a change in the number of visible satellites, we obtain an iterable containing information about each of then in order to count them, reporting their number in the relevant TextView.

            if event == GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                status = self.manager.getGpsStatus(None)
                sats = status.getSatellites()
                i = 0
                for sat in sats:
                    i += 1
                self.satellites.setText("Satellites: " + str(i))

The addText method is a custom method that we use to append lines of text to the event log in the activity's user interface. The method accepts a string that it appends to the existing log text before updating the TextView that displays the log.

    @args(void, [String])
    def addText(self, text):
        self.log += text + "\n"