Google Location Services on Android

Please find the corresponding udacity course here.

Table of Contents

  1. Play services
  2. Location and context
  3. Location example
  4. Activity recognition example
  5. Geofencing

Play services

Use them by adding to build.gradle:

compile 'com.google.android.gms:play-services:10.2.1'

Always use full version number like 10.2.1

Then add to the Manifest:

<meta-data 
	android:name="com.google.gms.version"
	android:value="@integer/google_play_services_version"/>

Create GoogleApiClient in onCreate()

Connect GoogleApiClient in onStart() to Location Service (or other relevant API)

Overwrite onConnectionFailed() and onConnectionSuspend() in Location Service

Write onConnected() and create a LocationRequest that queries the Location Service

Write onLocationChanged() and get the Location object which you can then use

Location and context

Fused Location Provider

Access sensors to determine your location by analyzing your

Fine and Coarse Location

Fine

Fine location involves using GPS, Cell and WiFi to get the most accurate possible position. Costing you extra battery life.

Add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> to you manifest for fine location.

Coarse

Coarse location uses Cell or WiFi signal. Is less fine, but costs less battery.

Add <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> to you manifest for coarse location.

Activity Recognition

Access sensors to determine your activity. It determines if you are

Location example

Add implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener to your Activity.

Add the following variables:

private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest;

In onCreate() build your GoogleApiClient by

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

In onStart() add

mGoogleApiClient.connect();

In onStop() add

if (mGoogleApiClient.isConnected()) {
	mGoogleApiClient.disconnect();
}

In onConnected() add

mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

This updates location periodically. Use last location to get location only once

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

Other priorities are

The interval within you read the sensors has a huge impact on battery life. Do you really need to update your location every second? Try to get the best for an appropriate user experience.

Grab your location from onLocationChanged(Location location)

Activity recognition example

Work-Queue-Processor design pattern. Example: IntentService (start a service, service handles each intent using a worker thread, stops itself when it runs out of work). Read here for more details.

  1. Create an Intent Service DetectedActivitiesIntentService extends IntentService
    1. Implement onHandleIntent()
    2. Add service to your manifest.xml
  2. Implement your MainAcitvity
    1. Make your activity implement ConnectionCallbacks and OnConnectionFailedListener
    2. Implement onConnected
    3. Implement onConnectionSuspended
    4. Implement onConnectionFailed
  3. Create nested BroadcastReceiver within your MainAcitvity
    1. Implement onReceive
    2. Declare Receiver class on MainActivity as member variable mBroadCastReceiver
    3. Instantiate Receiver variable in
  4. Set up the GoogleApiClient
    1. Connect and disconnect the client in onStart()/onStop()
    2. Unregister the broadcast receiver in onPause()
    3. Register the broadcast receiver in onResume()

Geofencing

Draw a virtual fence around a location in the real world and generate events when devices enter or exit the fence. Here are some examples:

Properties

Define a Geofence with the builder on the Geofence object and the following properties

Create a GeofencingRequest with addGeoFences holding an ArrayList of Geofences.