Sunday, November 23, 2014

Coursera Location Exercise - week 7

The screencast for the assignment demonstrates a ListView containing a set of Place Badges.

There is a context menu in this application with a list of preconfigured locations.  The user will click on the context menu and select one of the menu items, specifically "Place One", "Place Two," or "Place No Country."

The application will be set up to listen for the context menu selections.  Moreover, when one of the menu items is chosen, the app will receive the corresponding location details.

The footer for the ListView displays the words "Get New Place." When the user clicks on the footer, the application will go out and retrieve  the place Badge based on the user's current location. Although,  when the application first starts, the user will not have any Place Badges.

Each Place Badge contains
  • a country flag
  • a country name
  • place name corresponding to the user's location when the Place Badge was acquired

For the case when the user clicks on the footer and the application does not already have a Place Badge for the location, the application will create and execute an AsyncTask subclass called PlaceDownloaderTask that acquires the data needed to create the Place Badge. As well, the application should prevent the user from clicking on the footer when the application has not selected  a valid user location.

The ListView documentation details how you work with this type of widget, such as dealing with footers.

The HelloAndroidWithMenus example application demonstrates how to of work with a context menu. 

The applications listens for location updates from a network connection. You have to create an account at Your username will need to be updated in

Coding details:

PlaceViewActivity extends ListActivity

- ListView placesListView is the list view used in the Activity
        placesListView. setFooterDividersEnabled(true);
                 - Enables or disables the drawing of the divider for footer views.
        mAdapter = new PlaceViewAdapter(getApplicationContext());

- private PlaceViewAdapter mAdapter;
implementation class provided,  extends BaseAdapter, makes use of a PlaceRecord class
- set up a Location Manager

(LocationManager) getSystemService(Context.LOCATION_SERVICE);

- private MockLocationProvider mMockLocationProvider : 
implementation class provided, has method to push a Location instance which specifies the longitude,latitude, altitude, time, elapsedRealtimeNanos. Note that the LocationManager class has a method called setTestProviderLocation()

On Resume : you start listening for location updates, get last known location if reading was recent

mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, mMinTime, mMinDistance, this);

mLastLocationReading = new Location(mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)); 

Context Menu change handler method :

onOptionsItemSelected(d(MenuItem item)) - pushes location to the MockLocationProvider

ClickListener for the footer:

get a location reading:
mLastLocationReading = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if there is a reading:
if ((currentLocation != null) && (ageInMilliseconds(currentLocation) > ageInMilliseconds(mLastLocationReading))) {
           mLastLocationReading = currentLocation;

if there was already a place for this location in list:
"You already have this location badge",

Callback method used by PlaceDownloaderTask - check if the place you received has details, if so add it to list adapter:

                 boolean b = place.getCountryName().equals("");
                 if (b==false){
                 }  else {
                    "There is no country at this location",                     Toast.LENGTH_LONG).show();

No comments:

Post a Comment