lecture 6 - 1 - Location maps and sensors.pdf
Document Details
Uploaded by LegendaryDecagon
2024
Tags
Related
- 2024 S2 IT2010 Mobile Application Development Lecture PDF
- Mobile Application Development Lecture Notes PDF
- Mobile Systems and Application - Mobile Application Development
- Introduction-to-Mobile-Application-Development-using-Android_final).pptx
- Introduction-to-Mobile-Application-Development-using-Android_final).pptx
- Mobile Application Development Digital Notes (2022-2023) PDF
Full Transcript
CSIT242Mobile Application Development LECTURE 6 - 1 – LOCATION MAPS AND SENSORS 1 CSIT242 - SIM 2024 Lecture Plan 1. Android – Location & Google Map API 2. Android supported sensors 3. iOS – Location & Maps 4. iOS supported sensors 2 CSIT242 - SIM 2024 Android – Requesting Runtime Permissions The pu...
CSIT242Mobile Application Development LECTURE 6 - 1 – LOCATION MAPS AND SENSORS 1 CSIT242 - SIM 2024 Lecture Plan 1. Android – Location & Google Map API 2. Android supported sensors 3. iOS – Location & Maps 4. iOS supported sensors 2 CSIT242 - SIM 2024 Android – Requesting Runtime Permissions The purpose of a permission is to protect the privacy of an Android user Android apps must request permission to access sensitive user data (such as contacts and SMS), as well as certain system features (such as camera and internet) Depending on the feature, the system might grant the permission automatically (install-time permissions) or might prompt the user to approve the request (runtime permissions) 3 CSIT242 - SIM 2024 https://developer.android.com/guide/topics/permissions/overview#normal Android – Requesting Runtime Permissions Install-time permissions - give limited access to restricted data or restricted data or resources with very little risk to the user's privacy or the operation of other apps app store presents an install-time permission notice to the user in app's details page automatically granted at install time Normal permissions - access to data and actions that extend beyond your app's sandbox Signature permissions - grants permission to an app only when the app is signed by the same certificate as the app or the OS that defines the permission Run-time (Dangerous) permissions - the app wants data or resources that involve the user's sensitive information, or could potentially affect the operation of other apps The user has to explicitly grant the permission to the app The app must prompt the user to grant permission at runtime These permissions can be revoked later on Special permissions Permission groups - consist of a set of logically related permissions 4 CSIT242 - SIM 2024 Android – Requesting Runtime Permissions Request App permissions declare that the app needs a permission by putting element in the app manifest file check whether the permission is granted (before performing an operation that requires that permission) – use ContextCompat.checkSelfPermission(…) method Provide an explanation if the user has already denied that permission request (shouldShowRequestPermissionRationale() returns true/false if the user has previously denied the request and/or selected the “Don't ask again option”) Request permission - use requestPermissions(…) method When the user responds to the app's permission request, the system invokes the app's onRequestPermissionsResult(…) method 5 CSIT242 - SIM 2024 Geocoding and Reverse-geocoding Latitude and Longitude are the units that represent the coordinates at geographic coordinate system, so we can specify virtually any point on earth Just like every actual house has its address (which includes the number, the name of the street, city, etc), every single point on the surface of earth can be specified by the latitude and longitude coordinates Geocoding (forward-geocoding) is the process of converting a textual based geographical location (such as a street address) into geographical coordinates expressed in terms of longitude and latitude Reverse-geocoding involves the translation of geographical coordinates into a human readable address string Northfields Ave, Wollongong, NSW 2522 6 CSIT242 - SIM 2024 latitude: -34.405404 longitude: 150.878430 Android – Location & Context Location and maps-based apps offer a compelling experience on mobile devices Applications that have “location-related” features are using the classes of the android.location package, Google Location Services API, or the Google Maps Android API Main component of the location framework (android.location package) is the LocationManager system service, which provides APIs to determine location and bearing of the underlying device (if available) The Google Location Services API, part of Google Play services, is the preferred API a simpler API, higher accuracy, automated location tracking, low-power geofencing, and activity recognition new features such as activity detection that are not available in the location framework API 7 CSIT242 - SIM 2024 Android – Location permissions Apps that use location services must request location permissions Android offers three location permissions: ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION ACCESS_BACKGROUND_LOCATION (on Android 10+ / API level 29+) Chosen permission determines the accuracy of the location returned by the API or access on the device location while the app is in the background... If the app targets Android 10 or higher, must be declared the ACCESS_BACKGROUND_LOCATION permission in the app's manifest file and receive user permission in order to receive regular location updates while the app is in the background. If the app doesn't require location access while running in the background, it's highly recommended not to request ACCESS_BACKGROUND_LOCATION. 8 CSIT242 - SIM 2024 Android – Getting the Last Known Location Using the Google Play services location APIs, the app can request the last known location of the user's device The fused location provider (location APIs in Google Play services) can be used for retrieving the device’s last known location Fused Location Provider manages the underlying location technology and provides a simple API so can be specified requirements at a high level (like high accuracy or low power) optimizes the device's use of battery power 9 CSIT242 - SIM 2024 Android – Getting the Last Known Location To get the last known location of the device, an instance of the Fused Location Provider Client needs to be created The getLastLocation() method can be used to retrieve the device location The precision of the location returned by this call is determined by the permission setting in the app manifest The getLastLocation() method returns a Task that can be used to get a Location object with the latitude and longitude coordinates of a geographic location fusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener() { @Override public void onSuccess(Location location) { //Got last known location. In some situations this can be null. if (location != null) { // Logic to handle location object } } }); 10 CSIT242 - SIM 2024 Android – Getting the Last Known Location The location object may be null in the following situations: Location is turned off in the device settings (disabling location also clears the cache) The device never recorded its location (new device or restoring to factory settings) Google Play services on the device has restarted - there is no active Fused Location Provider client that has requested location after the services restarted The applications that use location, besides the geographical location (latitude and longitude), may require to get: the bearing (horizontal direction of travel), altitude, or velocity of the device These information are also available in the Location object (retrieved from the fused location provider) 11 CSIT242 - SIM 2024 Android – Change Location Settings LocationRequest objects are used to request a quality of service for location updates from the FusedLocationProvider API LocationRequest include methods like: ◦ setIntervalMillis(long) - sets the interval (milliseconds) for location updates ◦ setMinUpdateIntervalMillis(long) – sets the minimal interval of location updates ◦ setPriority(int) - sets the priority of the request: ◦ Priority.PRIORITY_BALANCED_POWER_ACCURACY - request for location precision is balanced between location accuracy and power usage ◦ Priority.PRIORITY_HIGH_ACCURACY – request for the most precise location possible ◦ Priority.PRIORITY_LOW_POWER - request that favours low power usage at the possible expense of location accuracy Priority.PRIORITY_PASSIVE - ensures that no extra power will be used to derive locations (will only receive locations calculated on behalf of other clients) 12 CSIT242 - SIM 2024 Android – Receiving Location Updates Regular location updates can be started by calling requestLocationUpdates() Based on the form of the request, the fused location provider can invoke the LocationCallback.onLocationResult() callback method and passes it a list of Location objects, or issues a PendingIntent that contains the location in its extended data The location updates can be stopped when the activity is no longer in focus This will reduce power consumption To stop location updates, can be called removeLocationUpdates() method 13 CSIT242 - SIM 2024 Android – Displaying a Location Address To get the location from the address and address from location Geocoder class (from android.location package) can be used The getFromLocation() method - accepts a latitude and longitude, and returns a list of addresses The getFromLocationName() method – accept the name of a place ("Wollongong", " Wollongong, Australia”, "Northfields Avenue, Wollongong, Australia",…) and returns an array of Addresses that are known to describe the named location The returned address object will be localized for the locale (specific geographical, political, or cultural region), and can be used to extract the latitude and longitude of the address 14 CSIT242 - SIM 2024 Android – Google Maps Android API The Google Maps Android API allows the inclusion of maps and customized mapping information in the app Using Google Maps Android API maps can be embedded into an activity as a fragment with a simple XML snippet Key developer features 3D maps; indoor, satellite, terrain, and hybrid maps; vector-based tiles for efficient caching and drawing; animated transitions add markers onto the map to indicate special points of interest for the users; define custom colours or icons for the map markers draw polylines and polygons to indicate paths or regions; provide complete image overlays ability to control the rotation, tilt, zoom, and pan properties of the "camera" perspective of the map add Street View to the app 15 CSIT242 - SIM 2024 Android – Google Maps Android API Maps, add to the app with the Google Maps Android API, are based on Google Maps data The API automatically handles access to Google Maps servers, data downloading, map display, and touch gestures on the map The key class in the Google Maps Android API is MapView. A MapView displays a map with data obtained from the Google Maps service MapView advantages provides all of the UI elements necessary for users to control the map captures keypresses and touch gestures to pan and zoom the map automatically the app can also use MapView class methods to control the map programmatically and draw a number of overlays on top of the map can handle network requests for additional maps tiles 16 CSIT242 - SIM 2024 Android – Sensors Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions These sensors are capable of providing raw data with high precision and accuracy, and are useful for monitoring three-dimensional device movement or positioning, or changes in the ambient environment near a device For example, a game might track readings from a device's gravity sensor to infer complex user gestures and motions, such as tilt, shake, rotation, or swing A weather application might use a device's temperature sensor and humidity sensor to calculate and report the dewpoint 17 CSIT242 - SIM 2024 Android – Sensors The Android platform supports three broad categories of sensors: Motion, Position and Environmental Motion sensors This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors Motion sensors are useful for monitoring device movement, such as tilt, shake, rotation, or swing These sensors measure acceleration forces and rotational forces along three axes The movement is usually a reflection of direct user input (monitoring motion relative to the device's frame of reference or the application's frame of reference), or a reflection of the physical environment in which the device is sitting (monitoring motion relative to the world's frame of reference) 18 CSIT242 - SIM 2024 Android – Sensors Position sensors This category includes orientation sensors and magnetometers Geomagnetic field sensor and the accelerometer sensors measure the physical position of a device (in the world's frame of reference) The Android platform also provides a sensor that determines how close the face of a device is to an object (proximity sensor) Environmental sensors This category includes barometers, photometers, and thermometers These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity With the exception of the light sensor, which most device manufacturers use to control screen brightness, environment sensors are not always available on devices 19 CSIT242 - SIM 2024 Android – Sensors Some of the sensors are hardware-based and some are software-based Hardware-based sensors are physical components built into a handset or tablet device They derive their data by directly measuring specific environmental properties, such as acceleration, geomagnetic field strength, or angular change Software-based sensors are not physical components, although they mimic hardware-based sensors They derive their data from one or more of the hardware-based sensors and are sometimes called virtual sensors or synthetic sensors The orientation sensor type is examples of software-based sensors Depending on the device some sensors can be both hardware of software based, or the device can have more than one sensor of a given type For example gravity sensor can be hardware or software based; also, a device can have two gravity sensors, each one having a different range 20 CSIT242 - SIM 2024 Android – Sensors Android sensor framework (part of android.hardware package) allows access to the sensors (available on the device) and raw sensor data The sensor framework provides several classes and interfaces for performing a wide variety of sensor-related tasks like: Determine which sensors are available on a device Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power requirements, and resolution Acquire raw sensor data and define the minimum rate at which you acquire sensor data Register and unregister sensor event listeners that monitor sensor changes Sensor availability varies from device to device, but it can also vary between Android versions. 21 CSIT242 - SIM 2024 Android – Sensors The sensor framework is part of the android.hardware package and includes the following classes and interfaces: SensorManager – used to create an instance of the sensor service It provides various methods for accessing and listing sensors, registering and unregistering sensor event listeners, acquiring orientation information, and several sensor constants used to report sensor accuracy, set data acquisition rates, and calibrate sensors Sensor - used to create an instance of a specific sensor It provides various methods that let you determine a sensor's capabilities SensorEvent – used to create a sensor event object A sensor event object includes the raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp for the event SensorEventListener - interface used to create two callback methods that receive notifications (sensor events) when sensor values change or when sensor accuracy changes 22 CSIT242 - SIM 2024 Android – Sensors Android does not specify a standard sensor configuration for devices Device manufacturers can incorporate any sensor configuration that they want into their Android-powered devices devices can include a variety of sensors in a wide range of configurations There are two options for ensuring that a given sensor is present on a device: Detect sensors at runtime and enable or disable application features as appropriate Use Google Play filters to target devices with specific sensor configurations 23 CSIT242 - SIM 2024 Android – Sensors Useful notes for creating apps that use sensors: Always verify that a sensor exists on a device before you attempt to acquire data from it Be sure to unregister a sensor's listener when you are done using the sensor or when the sensor activity pauses If a sensor listener is registered and its activity is paused, the sensor will continue to acquire data and use battery resources unless you unregister the sensor Be sure to choose a delivery rate that is suitable for the application or use-case Sensors can provide data at very high rates, so sending extra data wastes system resources and uses battery power Sensor data can change at a high rate, which means the system may call the onSensorChanged(SensorEvent) method quite often - do as little as possible within the onSensorChanged(SensorEvent) method (don't block it!) 24 CSIT242 - SIM 2024