•To stop location updates, can be called removeLocationUpdates()method.19
Android –Receiving Location Updates•Step 6 - Optional - Save the State of the Activity •A change to the device's configuration, such as a change in screen orientation or language, can cause the current activity to be destroyed. •The app must therefore store any information it needs to recreate the activity. One way to do this is via an instance state stored in a Bundle object.20
Android –Displaying a Location Address•Step 1 - Get a Geographic Location•The last known location of the device is a useful starting point for the address lookup feature. The getLastLocation()method, provided by the fused location provider, can be used to find the latest location of the device.•To access the fused location provider an instance of the Google Play services API client need to be created.•Permission (in Manifest):<uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION"/>21
Android –Displaying a Location Address•Step 2 - Define an Intent Service to Fetch the Address•The getFromLocation()method provided by the Geocoderclass accepts a latitude and longitude, and returns a list of addresses. •The method is synchronous, and may take a long time to do its work, so it shouldn’t be call from the main, user interface (UI) thread of the app.•The IntentServiceclass provides a structure for running a task on a background thread. Using this class, can be handled a long-running operation without affecting the UI's responsiveness.•FetchAddressIntentServiceclass extends IntentService. This class is address lookup service. The service uses a Geocoderto fetch the address for the location and sends the results to the ResultReceiverin the activity that started the service.22
Android –Displaying a Location Address•Step 3 - Start the Intent Service•The intent service runs in the background and is responsible for fetching the address corresponding to a given geographic location. •When the service is started, the Android framework instantiates and starts the service if it isn't already running, and creates a process if needed. •If the service is already running then it remains running. Because the service extends IntentService, it shuts down automatically when all intents have been processed.•Step 4 - Receive the Geocoding Results•The intent service has handled the geocoding request, and uses a ResultReceiverto return the results to the activity that made the request. •In the activity that makes the request, an AddressResultReceiverthat extends ResultReceivercan be defined to handle the response from FetchAddressIntentService.•The result includes a numeric result code (resultCode) as well as a message containing the result data (resultData).
You've reached the end of your free preview.
Want to read all 60 pages?
- Geographic coordinate system, Google Maps