CS5530 Mobile/Wireless Systems GPS/Location in Android Yanyan - - PowerPoint PPT Presentation

cs5530 mobile wireless systems gps location in android
SMART_READER_LITE
LIVE PREVIEW

CS5530 Mobile/Wireless Systems GPS/Location in Android Yanyan - - PowerPoint PPT Presentation

CS5530 Mobile/Wireless Systems GPS/Location in Android Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang UC. Colorado Springs CS5530 Ref. CN5E, NT@UW, WUSTL Android Location Interface 1. Request permission in


slide-1
SLIDE 1
  • Ref. CN5E, NT@UW, WUSTL

CS5530

CS5530 Mobile/Wireless Systems GPS/Location in Android

Yanyan Zhuang

Department of Computer Science http://www.cs.uccs.edu/~yzhuang

  • UC. Colorado Springs
slide-2
SLIDE 2

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

2 CS5530

  • 1. Request permission in AndroidManifest.xml

and code (Marshmallow+)

  • 2. Create a Location Manager
  • 3. Register a listener with the Location Manager

to receive location updates

  • 4. Define the listener that responds to location

updates (similar to IntentFilter and registerReceiver)

slide-3
SLIDE 3

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

3 CS5530

  • Permission
  • <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

/> <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. --> <uses-feature android:name="android.hardware.location.gps" />

  • If app targets Android 5.0 (API level 21) or higher, must declare that your app

uses android.hardware.location.gps hardware feature in the manifest file

slide-4
SLIDE 4

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

4 CS5530

  • Create a location manager
  • LocationManager locationManager = (LocationManager)

this.getSystemService(Context.LOCATION_SERVICE);

  • Before requesting location updates, must check

permission

  • if (checkPermission(this)) {

locationManager.requestLocationUpdates(….); } else{ ActivityCompat.requestPermissions(this, …); }

slide-5
SLIDE 5

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

5 CS5530

  • Before requesting location updates, checking permission
  • public static boolean checkPermission(final Context context) {

return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; }

  • if (checkPermission(this)) {

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); locationManager.addGpsStatusListener(gpsStatusListener); }else{ ActivityCompat.requestPermissions(this, PERMS_INITIAL, 111); }

slide-6
SLIDE 6

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

6 CS5530

  • Register a listener (to request location updates)
  • locationManager.requestLocationUpdates(

LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);

  • // The minimum distance to change Updates in meters

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 m // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

slide-7
SLIDE 7

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

7 CS5530

  • Define the listener that responds to location updates
  • LocationListener locationListener = new LocationListener() {

@Override public void onLocationChanged(Location location) { if (location != null) myLocation = location; } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } };

slide-8
SLIDE 8

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

8 CS5530

  • Get information about satellites
  • Register a satellite listener

} locationManager.addGpsStatusListener(gpsStatusListener)

  • Implement gpsStatusListener to respond to satellite status changes

} private GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {

@Override public void onGpsStatusChanged(int event) { …... } };

slide-9
SLIDE 9

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

9 CS5530

  • GPS satellite listener
  • private GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {

@Override public void onGpsStatusChanged(int event) { if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){ if (checkPermission(getApplicationContext())) { GpsStatus gpsstatus = locationManager.getGpsStatus(null); Iterable<GpsSatellite> gpsit = gpsstatus.getSatellites(); int numsat = 0; for (GpsSatellite sat : gpsit) { Log.v(TAG, Float.toString(sat.getAzimuth())); Log.v(TAG, Float.toString(sat.getElevation())); Log.v(TAG, Integer.toString(sat.getPrn())); Log.v(TAG, Float.toString(sat.getSnr())); numsat++; } } } } };

slide-10
SLIDE 10

Android Location Interface

  • Ref. CN5E, NT@UW, WUSTL

10 CS5530

  • float getAzimuth()
  • Returns the azimuth of the satellite in degrees. The azimuth can vary

between 0 and 360

  • float getElevation()
  • Returns the elevation of the satellite in degrees. The elevation can vary

between 0 and 90

  • int getPrn()
  • Returns the PRN (pseudo-random number) for the satellite
  • float getSnr()
  • Returns the signal to noise ratio for the satellite.