Android 16: Location Kirk Scott
• • • • •
16.1 16.2 16.3 16.4 16.5
Introduction Google Services Google Play Services Location APIs Making Your App Location Aware
16.1 Introduction • This unit belongs to the third third of the course • It covers a topic that might be of some interest when developing your final project
• Unless you planned it up front, it’s unlikely that you could randomly add location • However, this is a worthwhile topic to consider since it’s part of what makes Android mobile devices so interesting
• The full title of the related section in the API guide is “Location and Sensors” • This section in the API guides consists of the subsections listed on the following overhead
• • • • • •
Location and Maps Location Strategies Sensors Overview Motion Sensors Position Sensors Environment Sensors
• What’s shown on the following two overheads is what you find if you go to the section on Location and Maps
• Location and Maps • Note: • This is a guide to the Android framework location APIs in the package android.location. • The Google Location Services API, part of Google Play Services, provides a more powerful, highlevel framework that automates tasks such as location provider choice and power management. • Location Services also provides new features such as activity detection that aren't available in the framework API. 11
• Developers who are using the framework API, as well as developers who are just now adding location-awareness to their apps, should strongly consider using the Location Services API. • To learn more about the Location Services API, see Google Location Services for Android.
• So the moral of the story is that this set of overheads will not cover any technical content • It will just cover the overview information on the services structure for supporting location in Android apps • It will conclude with the introduction to the training section, where the reader could go for technical content 13
16.2 Google Services
• Google Services • Google offers a variety of services that help you build new revenue streams, manage app distribution, track app usage, and enhance your app with features such as maps, sign-in, and cloud messaging. • Although these Google services are not included in the Android platform, they are supported by most Androidpowered devices. • When using these services, you can distribute your app on Google Play to all devices running Android 2.2 or higher, and some services support even more devices. 16
• Google Maps • Include the power of Google Maps in your app with an embeddable map view. • You can customize the map with markers and overlays, control the user's perspective, draw lines and shapes, and much more.
• Google Play In-App Billing • Build an app with a steady revenue stream that keeps users engaged by offering new content or virtual goods directly in your app. • All transactions are handled by Google Play Store for a simple user experience.
• Google+ • Allow users to sign in with their Google account, customize the user experience with Google+ info, pull people into your app with interactive posts, and add +1 buttons so users can recommend your content.
• Google Wallet Instant Buy • Provide fast and easy checkout in your app when selling physical goods and services. • Increase conversions by streamlining your purchase flow and reducing the amount of information your customers need to enter.
• Google Cloud Platform • Build and host the backend for your Android app at Google-scale. • With an infrastructure that is managed automatically, you can focus on your app. • Then, scale to support millions of users.
• Google Analytics • Measure your success and gain insights into how users engage with your app content by integrating Google Analytics. • You can track in-app purchases, the number of active users, interaction patterns, and much more.
• Google Cloud Messaging • Immediately notify your users about timely events by delivering lightweight messages from your web server. • There are no quotas or charges to use Google Cloud Messaging.
• Google Mobile Ads • Display ads from Google Mobile Ads offer you an alternative revenue opportunity that leverages multiple ad networks with targeted ads and several display formats.
16.3 Google Play Services
• Google Play Services • Give your apps more features to attract users on a wider range of devices. • With Google Play services, your app can take advantage of the latest, Google-powered features such as Maps, Google+, and more, with automatic platform updates distributed as an APK through the Google Play store. • This makes it faster for your users to receive updates and easier for you to integrate the newest that Google has to offer. 27
• Google Technology • Google Play services provides you with easy access to Google services and is tightly integrated with the Android OS. • Easy-to-use client libraries are provided for each service that let you implement the functionality you want easier and faster.
• Standard Authorization • All products in Google Play services share a common authorization API that leverages the existing Google accounts on the device. • You and your users have a consistent and safe way to grant and receive OAuth2 access tokens to Google services.
• Automatic Updates • Devices running Android 2.2 and newer and that have the Google Play Store app automatically receive updates to Google Play services. • Enhance your app with the most recent version of Google Play services without worrying about your users' Android version. 30
• To start integrating Google Play services into your app, follow the Setup guide.
How It Works • The Google Play services client library • The client library contains the interfaces to the individual Google services and allows you to obtain authorization from users to gain access to these services with their credentials.
• It also contains APIs that allow you to resolve any issues at runtime, such as a missing, disabled, or out-of-date Google Play services APK. • The client library has a light footprint if you use ProGuard as part of your build process, so it won't have an adverse impact on your app's file size. 33
• If you want to access added features or products, you can upgrade to a new version of the client library as they are released. • However, upgrading is not necessary if you don't care about new features or bug fixes. • We anticipate more Google services to be continuously added, so be on the lookout for these updates. 34
• The Google Play services APK • The Google Play services APK contains the individual Google services and runs as a background service in the Android OS. • You interact with the background service through the client library and the service carries out the actions on your behalf.
• An easy-to-use authorization flow is also provided to gain access to the each Google service, which provides consistency for both you and your users. • The Google Play services APK is delivered through the Google Play Store, so updates to the services are not dependent on carrier or OEM system image updates. 36
• In general, devices running Android 2.2 (Froyo) or later and have the Google Play Store app installed receive updates within a few days. • This allows you to use the newest APIs in Google Play services and reach most of the devices in the Android ecosystem (devices older than Android 2.2 or devices without the Google Play Store app are not supported). 37
• The Google Play services APK on user devices receives regular updates for new APIs, features, and bug fixes.
• The benefits for your app • Google Play services gives you the freedom to use the newest APIs for popular Google services without worrying about device support. • Updates to Google Play services are distributed automatically by the Google Play Store and new versions of the client library are delivered through the Android SDK Manager. 39
• This makes it easy for you to focus on what's important: your users' experience. • To get started, set up the SDK and check out the various products in the Google Play services platform now!
16.4 Location APIs
• Location APIs • The location APIs make it easy for you to build location-aware applications, without needing to focus on the details of the underlying location technology. • They also let you minimize power consumption by using all of the capabilities of the device hardware. 43
• To get started, first set up the Google Play services SDK. • You can learn how to use the APIs in the training class Making Your App Location Aware, and details are available in the Location API reference.
Key Developer Features • Fused location provider • The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs.
• Simple APIs: • Lets you specify high-level needs like "high accuracy" or "low power", instead of having to worry about location providers. • Immediately available: • Gives your apps immediate access to the best, most recent location.
• Power-efficiency: • Minimizes your app's use of power. • Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.
• Versatility: • Meets a wide range of needs, from foreground uses that need highly accurate location to background uses that need periodic location updates with negligible power impact.
• Geofencing APIs • Lets your app setup geographic boundaries around specific locations and then receive notifications when the user enters or leaves those areas.
• Simple but powerful APIs: • Allows batch addition and removal of geofences. • Ability to manage multiple geofences at the same time. • Ability to filter alerts for both entry and exit or entry only or exit only.
• Optimized for battery: • Adjusts location updates based on user’s proximity to the geofence and user’s modality (still, walking, driving, and so on).
• Activity recognition • With apps becoming increasingly contextual, understanding what the user is doing is critical to surfacing the right content. • The Activity recognition API makes it easy to check the user’s current activity—still, walking, cycling, and in-vehicle—with very efficient use of the battery. 52
• Optimized for battery: • Uses low-power sensors to recognize the user's current physical activity.
• Enhances other services with context: • Great for adding movement awareness to location awareness. • Apps can adjust the amount of location awareness they provide, based on the current user movement. • For example, a navigation app can request more frequent updates when the user is driving. 54
• Features for advanced applications: • For advanced applications that want to do their own post-processing, this API also makes available confidence values for each of the activities. • It also includes two activities that indicate unreliable measurements: unknown and tilt.
16.5 Making Your App Location Aware • [Note: This section is taken from the training part of the developers’ Web site.]
• Making Your App Location-Aware • One of the unique features of mobile applications is location awareness. • Mobile users bring their devices with them everywhere, and adding location awareness to your app offers users a more contextual experience.
• The new Location Services API available in Google Play services facilitates adding location awareness to your app with automated location tracking, geofencing, and activity recognition. • This API adds significant advantages over the plaform's location API.
• This class [training class, not API class] shows you how to use Location Services in your app to get the current location, get periodic location updates, look up addresses, create and monitor geofences, and detect user activities. • The class includes sample apps and code snippets that you can use as a starting point for adding location awareness to your own app.
• Note: Since this class is based on the Google Play services client library, make sure you install the latest version before using the sample apps or code snippets. • To learn how to set up the client library with the latest version, see Setup in the Google Play services guide.
Lessons • • • •
Retrieving the Current Location Learn how to retrieve the user's current location. Receiving Location Updates Learn how to request and receive periodic location updates. • Displaying a Location Address • Learn how to convert a location's latitude and longitude into an address (reverse geocoding). 61
• Creating and Monitoring Geofences • Learn how to define one or more geographic areas as locations of interest, called geofences, and detect when the user is close to or inside a geofence.
• Recognizing the User's Current Activity • Learn how to recognize the user's current activity, such as walking, bicycling, or driving a car, and how to use this information to modify your app's location strategy.
• Testing Using Mock Locations • Learn how to test a location-aware app by injecting mock locations into Location Services. • In mock mode, Location Services sends out mock locations that you inject instead of sensor-based locations.
Summary and Mission • This is where this set of overheads ends • For the time being, none of the individual classes will be covered • The end of the semester is nearing • If anyone is interested, they will have to pursue this on their own