![Page 1: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/1.jpg)
Understanding GPS Accuracy Mastering Android SDK Geolocation
Andy Gup www.andygup.net
@agup
![Page 2: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/2.jpg)
Goals/Objectives
Go beyond the native SDK documentation Use Cases, Use Cases, Use Cases Coding Patterns Android GPS Test Tool
Save you hours, days or weeks of ramp-up
![Page 3: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/3.jpg)
Assumptions
Familiar with Android SDK Worked with Android projects Understand Java
![Page 4: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/4.jpg)
Who am I? Andy Gup Developer Evangelist www.andygup.net github.com/andygup [email protected] @agup
![Page 5: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/5.jpg)
This is not about…
New Google Play Services SDK Fused Location Provider Activity Recognition Geofencing APIs http://developer.android.com/google/play-services/location.html
![Page 6: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/6.jpg)
Accuracy
Depends on many things: Device type Which providers enabled Internet connectivity Cellular provider
![Page 7: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/7.jpg)
Accuracy
Depends on many things: In a parked car In a moving car In a building In a parking garage
![Page 8: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/8.jpg)
Accuracy
![Page 9: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/9.jpg)
![Page 10: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/10.jpg)
![Page 11: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/11.jpg)
??
![Page 12: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/12.jpg)
Define accuracy
Time Distance Speed Heading
![Page 13: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/13.jpg)
Reject bad results
5 secs East (270 deg)
![Page 14: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/14.jpg)
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
![Page 15: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/15.jpg)
Android GPS Test Tool https://github.com/Esri/android-gps-test-tool
![Page 16: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/16.jpg)
package
android.location
![Page 17: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/17.jpg)
![Page 18: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/18.jpg)
android.location.LocationManager
Provides access to system location services.
![Page 19: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/19.jpg)
android.location.LocationProvider
Provides geographic location Has a set of criteria GPS_PROVIDER NETWORK_PROVIDER PASSIVE_PROVIDER
![Page 20: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/20.jpg)
Requesting Updates android.location.LocationManager
requestLocationUpdates() requestSingleUpdate()
![Page 21: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/21.jpg)
Requesting Updates
requestLocationUpdates( provider,
minTime, /* ms */
minDistance, /* meters */
listener
);
![Page 22: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/22.jpg)
Requesting Updates (GPS) if(gpsProviderEnabled == true){ _locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
10000 /* minTime */,
10 /* minDistance */,
_locationListenerGPSProvider
);
}
![Page 23: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/23.jpg)
Six types of location data
Real-time GPS Network Cached GPS Network Passive NMEA
![Page 24: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/24.jpg)
Location data: Real-time GPS
Latitude - (decimal degrees) Longitude - (decimal degrees) Time - (UTC since Jan 1, 1970) Bearing - (degrees) Speed - (meters/second) Altitude - (meters >= sea level) Accuracy - (meters)
![Page 25: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/25.jpg)
Location data: Real-time GPS Location[ mProvider=gps, mTime=1374081473000, mLatitude=39.91824753, mLongitude=-105.10395774, mHasAltitude=true, mAltitude=1065.0, mHasSpeed=true, mSpeed=0.0, mHasBearing=false, mBearing=0.0, mHasAccuracy=true, mAccuracy=72.0, mExtras=Bundle [mParcelledData.dataSize=44]]
![Page 26: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/26.jpg)
Location data: Real-time Network
Latitude - (decimal degrees) Longitude - (decimal degrees) Time - (UTC since Jan 1, 1970) Accuracy - (meters)
![Page 27: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/27.jpg)
Location[ mProvider=network, mTime=1373643571572, mLatitude=33.6985455, mLongitude=-117.9914674, mHasAltitude=false, mAltitude=0.0, mHasSpeed=false, mSpeed=0.0, mHasBearing=false, mBearing=0.0, mHasAccuracy=true, mAccuracy=55.161, mExtras=Bundle [mParcelledData.dataSize=212]]
Location data: Real-time Network
![Page 28: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/28.jpg)
Location: Cached GPS
Latitude - (decimal degrees) Longitude - (decimal degrees) Time - (UTC since Jan 1, 1970) Bearing - (degrees) Speed - (meters/second) Altitude - (meters >= sea level) Accuracy - (meters)
![Page 29: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/29.jpg)
Location data: Cached Network
Latitude - (decimal degrees) Longitude - (decimal degrees) Time - (UTC since Jan 1, 1970) Accuracy - (meters)
![Page 30: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/30.jpg)
Location data: Passive
Latitude - (decimal degrees) Longitude - (decimal degrees) Time - (UTC since Jan 1, 1970) Bearing - (degrees) ??? Speed - (meters/second) ??? Altitude - (meters >= sea level) ?? Accuracy - (meters) ???
![Page 31: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/31.jpg)
Location data: NMEA
addNmeaListener(GpsStatus.NmeaListener);
$GNGSA,A,2,67,68,78,,,,,,,,,,2.4,2.2,0.8*23
![Page 32: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/32.jpg)
7 Technical use cases
Cold start Warm start Minimized Passive Snapshot Intermittent Continuous
![Page 33: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/33.jpg)
Technical Use Case: Cold start
Phone rebooted Updated phone OS Potential for no cached values Potential for large inaccuracy
![Page 34: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/34.jpg)
Cold start
![Page 35: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/35.jpg)
Technical Use Case: Warm start
Cached locations available GPS has been run recently
![Page 36: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/36.jpg)
Warm start
Compare Cached network & GPS Check timestamps!!
![Page 37: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/37.jpg)
Example: ~10 seconds Accuracy 71 meters
Warm start
![Page 38: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/38.jpg)
Technical Use Case: Minimized
App in background GPS can be run Can kill battery quickly
![Page 39: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/39.jpg)
Dependent on some other app Only receives if other app requests location No guarantees!
Technical Use Case: Passive
<receiver android:name=".receivers.PassiveLocationChangedReceiver"/>
![Page 40: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/40.jpg)
Technical Use Case: Snapshot
One-time location >= minimum accuracy Example: Standing indoors ~2 mins Accuracy 20 meters
![Page 41: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/41.jpg)
Technical Use Case: Snapshot
One-time location >= minimum accuracy Example: Next to building ~30 seconds Accuracy 3 meters
![Page 42: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/42.jpg)
Technical Use Case: Intermittent
No movement detected for period of time Adjusts minTime & minDistance Temporarily sleep GPS Keeps GPS ‘warm’
![Page 43: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/43.jpg)
Technical Use Case: Continuous
GPS stays on Highest level of accuracy Most battery usage
![Page 44: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/44.jpg)
End-User scenarios - Continuous
Walking Running Hiking Biking Driving
![Page 45: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/45.jpg)
Biking Example Technical Requirements Start minTime = 0 minDistance = 0 if accuracy <= 20 meters speed > 5 mph && speed < 45 mph time > 2 minutes power source = battery then reset location updates minTime = 5000 minDistance = 10
![Page 46: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/46.jpg)
End-User scenarios - Snapshot
Find coffee Find nearby places Get start location for driving directions
![Page 47: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/47.jpg)
Find nearby coffee shops
Example Technical Requirements Start minTime = 0 minDistance = 0 if accuracy <= 1000 meters then shutoff location
![Page 48: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/48.jpg)
Questions? Andy Gup Developer Evangelist www.andygup.net github.com/andygup [email protected] @agup
![Page 49: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/49.jpg)
Bonus Slides
![Page 50: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/50.jpg)
Step 1: AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
![Page 51: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/51.jpg)
Step 2a: retrieve LocationManager
LocationManager _locationManager = Context.getSystemService(Context.LOCATION_SERVICE);
![Page 52: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/52.jpg)
Step 2b: verify providers
final Boolean gpsProviderEnabled = _locationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER);
final Boolean networkProviderEnabled = _locationManager.isProviderEnabled(
LocationManager.NETWORK_PROVIDER);
final Boolean passiveProviderEnabled = _locationManager.isProviderEnabled(
LocationManager.PASSIVE_PROVIDER);
![Page 53: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/53.jpg)
Step 3: Last Known Location Location _lastKnownLocationNetworkProvider = _locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER); Location _lastKnownLocationGPSProvider = _locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER); if(_lastKnowLocationNetworkProvider != null){ final double cachedNetworkLatitude = _lastKnownLocationNetworkProvider.getLatitude(); final double cachedNetworkLongitude = _lastKnownLocationNetworkProvider.getLongitude(); }
![Page 54: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/54.jpg)
Step 4a: set GPS Listener _gpsLocationListener = new LocationListener() { @Override
public void onLocationChanged(Location location) { // TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) { // TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) { // TODO Auto-generated method stub
}
}
![Page 55: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/55.jpg)
Step 4b: set Network Listener _networkLocationListener = new LocationListener() { @Override
public void onLocationChanged(Location location) { // TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) { // TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) { // TODO Auto-generated method stub
}
}
![Page 56: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/56.jpg)
NOTE: multiple listeners
You will not be able to shut off a provider if it has multiple listeners.
![Page 57: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/57.jpg)
Step 5: Requesting Updates android.location.LocationManager
requestLocationUpdates() requestSingleUpdate()
![Page 58: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/58.jpg)
Requesting Updates
requestLocationUpdates( provider,
minTime, /* ms */
minDistance, /* meters */
listener
);
![Page 59: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/59.jpg)
Requesting Updates (GPS) if(gpsProviderEnabled == true){ _locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
10000 /* minTime */,
10 /* minDistance */,
_locationListenerGPSProvider
);
}
![Page 60: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/60.jpg)
Requesting Updates (Network) if(networkProviderEnabled == true){ _locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
10000 /* minTime */,
10 /* minDistance */,
_locationListenerNetworkProvider
);
}
![Page 61: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/61.jpg)
Requesting Updates (Passive) if(passiveProviderEnabled == true){ _locationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER,
10000 /* minTime */,
10 /* minDistance */,
_locationListenerPassiveProvider
);
}
![Page 62: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/62.jpg)
Requesting Updates minTime
milliseconds >= 0 if(minTime > 0 && minDistance)
minDistance
meters >=0 Less battery efficient than minTime
![Page 63: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/63.jpg)
Requesting Updates
![Page 64: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/64.jpg)
Step 6: process location updates @Override
public void onLocationChanged(Location location) { double lat = location.getLatitude();
double long = location.getLongitude();
double altitude = location.getAltitude();
float accuracy = location.getAccuracy();
float bearing = location.getBearing();
float speed = location.getSpeed();
}
![Page 65: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/65.jpg)
Best Provider via Criteria Criteria criteria = new Criteria(); criteria.setAccuracy(accuracy);
criteria.setCostAllowed(cost);
criteria.setPowerRequirement(power);
...
...
final String bestProviderName = _locationManager.getBestProvider(criteria, true);
![Page 66: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/66.jpg)
Best Provider by comparison if(_networkAccuracy > _gpsAccuracy && _gpsTime > _networkTime &&
_gpsTimeDiff > _MIN_UPDATE_TIME){
//Use network Location data
}
if(_gpsAccuracy > _networkAccuracy && _networkTime > _gpsTime &&
_networkTimeDiff > _MIN_UPDATE_TIME){
//use gps Location data
}
![Page 67: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/67.jpg)
Step 7: shutdown updates if(_locationManager != null && _locationListenerNetworkProvider != null && _locationListenerGPSProvider != null){ _locationManager.removeUpdates( _locationListenerNetworkProvider); _locationManager.removeUpdates( _locationListenerGPSProvider); _locationListenerNetworkProvider = null; _locationListenerGPSProvider = null; }
![Page 68: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/68.jpg)
Consuming real-time locations
Time to 1st result - Elapsed time - Accuracy value
Compare GPS vs Network
!==
![Page 69: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/69.jpg)
Consuming real-time locations
Subsequent results - Elapsed time - Accuracy value - Bearing - Distance traveled
![Page 70: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/70.jpg)
Streaming updates
Time between updates
![Page 71: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/71.jpg)
Streaming updates
Network vs GPS Example: 2093m vs 17m
![Page 72: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/72.jpg)
Consuming real-time locations
Handle bad locations Recovering from errors Lost connection: GPS Network
![Page 73: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/73.jpg)
Detect power on app startup
@Override
public void onCreate(Bundle savedInstanceState) {
Intent intent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
boolean isBatteryOn = intent.getIntExtra(
BatteryManager.EXTRA_PLUGGED, -1) > 0
}
![Page 74: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/74.jpg)
Detect power state change
BroadcastReciever
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
![Page 75: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/75.jpg)
Detect low battery <action android:name="android.intent.action.ACTION_BATTERY_LOW"/> <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
public class PowerStateChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) { boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);
if(batteryLow == true){
//Do something
}
}
}
![Page 76: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/76.jpg)
Detecting provider changes @Override public void onStatusChanged(String provider, in status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub }
![Page 77: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/77.jpg)
onStatusChanged Event
OUT_OF_SERVICE TEMPORARILY_UNAVAILABLE AVAILABLE
![Page 78: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/78.jpg)
onProviderDisabled Event
Let your user know Store the timestamp for comparison Attempt to reestablish connection/reset
![Page 79: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/79.jpg)
onProviderEnabled Event
Verify accuracy, timestamp Let user know data was interrupted
![Page 80: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/80.jpg)
Activity starting and stopping @Override
protected void onPause() { stopLocation();
super.onPause(); }
@Override
protected void onStop(){ super.onStop(); stopLocation();
}
@Override
protected void onResume() { super.onResume(); startLocation();
}
![Page 81: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/81.jpg)
Detect connectivity change (static)
![Page 82: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/82.jpg)
Helper methods: Distance Traveled android.location.Location
distanceBetween() distanceTo() bearingTo()
![Page 83: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/83.jpg)
Battery life
![Page 84: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/84.jpg)
Battery life
minTime > 0 Shutoff location when minimized. Shutoff location at min. accuracy Modify LocationManager on battery if movement stops for long periods under different usage conditions
![Page 85: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/85.jpg)
Keep screen turned on getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
![Page 86: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/86.jpg)
Mock Updates (Emulator)
DDMS Emulator Control
![Page 87: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/87.jpg)
Mock Updates (Device)
Android Manifest <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
![Page 88: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/88.jpg)
Mock Updates (Device)
Android Manifest <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
Device Settings
![Page 89: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/89.jpg)
Mock Updates (Device) https://github.com/andygup/mock-location-test-android
![Page 90: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/90.jpg)
![Page 91: Understanding GPS Accuracy - Mastering Android SDK …Goals/Objectives . Go beyond the native SDK documentation . Use Cases, Use Cases, Use Cases . Coding Patterns . Android GPS Test](https://reader031.vdocument.in/reader031/viewer/2022011902/5f0cdeca7e708231d4378a41/html5/thumbnails/91.jpg)
Privacy
Get legal advice Allow for opt-out Be clear about your privacy policy