Android From Scratch: Google Play Services

The Google Play Services library allows Android developers to easily connect to and use Google-powered features, and provides backwards compatibility for new features. There are two main parts to using Google Play Services, the Google services back end and the client app. In this tutorial you will learn about the various components within Google Play Services that are available for your apps, and how to configure them through the Google Developer Console.

Google Play Services is contained within an APK and runs as a background service on every Android device that connects to the Play Store. Because Google Play Services are distributed through the Play Store, they can be updated independent of phone manufacturer or carrier, allowing each device back to API 9 to stay up to date without unnecessary roadblocks. On average, Google Play Services will update on a device within a few days, and Google will not release an updated SDK to developers until the vast majority of devices have the latest APK.

Diagram of Google Play Services and Android

Components of Play Services

Over the years, Play Services has gone from a simple way to access new Google features in a backwards compatible way to a behemoth containing many different packages that can be used for a variety of features. Understanding what components are available in Google Play Services is the first step to knowing what can be used to improve your apps. Each component can be imported into your projects individually through your build.gradle file.

Google+ and Authentication

Play Services allows developers to integrate with Google+ by providing login authentication for Google accounts, the ability to post interactive posts and get information about a user's profile, content sharing, and the +1 button.

Addresses

Added in Play Services 4.3, the Addresses feature allows users to select and enter addresses from a UI component that will then populate all of the appropriate information into the app. This allows users to easily enter information for any address without having to rely on location services, which may be inaccurate or may not represent the desired address. 

App Indexing

App Indexing allows users to click web links, such as Google search result links, and have them open in an Android app that has been installed on their device. This feature allows users to have an experience that is optimized for their device and drives engagement with the mobile app.

App Invites

The App Invites package contains a UI component that will open in a mobile app and allow users to share a link to your application through email with contacts selected by the user. This helps drive more installs for your application.

Analytics

This package allows developers to easily integrate with Google Analytics. Using analytics, developers and businesses can learn about how users interact with their application, allowing developers to change sections to be more useful to users or add additional functionality in the more heavily trafficked sections of the app.

Actions

One of the simpler features in Google Play Services, the Actions package adds support for various types of intents used for Google services, such as search. 

Awareness

Available under the Context Manager package, the Awareness API allows developers to access various data sources from Google in order to create contextually aware applications. Data sources include weather, beacons, places, location, and activity recognition.

Awareness API representation

Google Cast

Recently upgraded to Cast SDK 3, this package allows developers to control content that is being played on a Google Cast device, such as an Android TV or Chromecast. In addition to being able to connect to Cast devices and sending content, this SDK provides UI components that conform to the Cast design guidelines.

Google Cloud Messaging

Cloud Messaging allows developers to send messages from a back-end service to client apps. These messages can be used to trigger notifications or start tasks in an app, as 4kb of payload data are allowed to be sent with every message.

Google Drive

Using the Google Drive API, you can create, edit and delete documents, search files, and pin documents to a device. When an item is pinned, the latest version will always be downloaded onto the device and will be available to all applications with access to the file while offline.

Google Fit

Google Fit allows developers to store fitness data for users in order to help users keep track of their exercise habits. All data stored through Google Fit is stored online and is accessible by the user from multiple devices. The user never has to worry about losing their information if they upgrade or change devices. 

When data is saved through Google Fit in your app, it is made available through the Google Fit app for users to view and keep track of their fitness habits.

Google Fit app displaying fitness history

Google Location and Activity Recognition

Using Google Play Services, you can gather information about the user's current location (latitude and longitude), speed, altitude, and bearing. In addition to location, the location package of Play Services allows developers to determine what action a user is taking, such as walking, running, riding a bike, or travelling in a car.

Google Maps

One of the most widely used features of Google Play Services, the Maps feature includes everything you need to display a Google Map in your application, place markers, draw on top of the map surface with either images or simple shapes, add indoor level overlays, and even display a street view. 

In addition to the standard Maps feature, Google has also provided an open sourced library called Map Utils that provides additional features, such as heat maps and clustered markers.

Google Maps on Android with markers

Google Ads

While many may think of Google as an internet search company, their true focus is advertising. Using Google Play Services, you can tap into Google's advertising network in order to display ads that are properly sized for your applications and, hopefully, generate some extra revenue.

Places

The Places API allows developers to access a wealth of information from Google to provide users with an experience tailored to their current location by using the names and information of places, rather than a set of coordinates. In addition to being able to simply obtain information, this feature provides some UI components for selecting places near the user, giving developers an easy-to-use out-of-the-box feature for their apps.

Places API Place Picker

Mobile Vision

Mobile Vision allows developers to analyze either still images or input from a camera to provide functionality to their apps. Using the Mobile Vision features, developers can analyze and detect faces to locate them in an image, determine the probability of eyes being opened or closed, and figure out how likely it is that the subjects of the image are smiling. 

This package also includes the ability to scan and interpret barcodes, and recently added the ability to analyze text in real time for translation, as seen in the Google Translate Android app.

Faces detected via the Vision API

Nearby

Nearby allows users to interact with either other devices or beacons in their general proximity. Using the Nearby Messages API, devices can communicate with each other through Bluetooth, ultrasound or Wi-Fi. Using the Nearby Connections API, devices can discover each other over a network. 

Finally, beacons can be used to broadcast information to nearby devices, such as URLs, that are then displayed as notifications on a user's device.

Example of Nearby Beacons broadcasting URLs

Panorama Viewer

Using the Panorama Viewer feature, you can create applications that can access images with panorama meta-data and display them using the device's built-in panorama viewer. Panorama images work similar to Google Street View images.

Play Game Services

Play Game Services provides a number of tools to improve the experience of your games and easily add new features. Play Game Services includes achievements, leader boards, multiplayer support, player analytics, game saves, events and quests, gifting, and video recording.

SafetyNet

Using SafetyNet, you can analyze a device's hardware and software to confirm that a device will work as expected when running your app. Devices are compared to pre-tested compatible devices by Google, and a result is returned to your app.

Android Pay

Using Google Play Services, you can integrate your app with Android Pay, allowing users to purchase physical products through your app. If your app only sells digital products, you should consider using Google Play's in-app purchases.

Android Wear

While UI components for Android Wear are available in the Wear support library, any phone-to-wearable communication is handled through Google Play Services using the Data Layer API for synced data, Message API for one-off messages, and the Node API for detecting connected devices. 

Firebase

Built on top of Google Play Services, Firebase encapsulates multiple Google features into one easy-to-use service. Firebase allows Android developers to quickly create a NoSQL database back end that allows for automatically syncing data across apps and the back end. In addition to an easy to use back end, Firebase adds support for analytics, Google ads, app indexing, cloud messaging, and app invites. 

Enabling Google Play Services

Before using Play Services in your Android application, some APIs will require that you use the Google API Console to set up the services that you will use. In this section you will learn how to enable APIs and create an API key for accessing them from an Android app. While accessing these APIs is beyond the scope of this article, you can find many tutorials on the Google Play Services components in the Envato Tuts+ tutorial listings.

Step 1: Creating a Project

To create a new project, click on the blue Create Project button in the top center of the screen. 

Google API Console Create Project

This presents you with a dialog that asks for a project name. There are some restrictions on what you can name your project as only letters, numbers, quotes, hyphens, spaces, and exclamation points are allowed characters.

Google API Console New Project screen

Once you hit Create, a dialog appears in the lower right corner of the page indicating that the project is being created. Once it has disappeared, your project can be accessed. You should see a screen similar to the following. If not, click on the Google APIs logo in the top left corner to be taken to the API manager screen.

Google API Console Overview screen

Step 2: Enabling APIs

Once you have created a project, you will need to enable the various Google APIs that will be used in your apps. You can do this by going into the search box and typing in the name of any API, or by selecting it from the popular APIs list on the Overview screen. Once you have selected an API to enable, click on the blue Enable button.

Awareness API message prompting you to create credentials

If this is the first API you have enabled, you will be prompted to create a set of credentials. 

Step 3: Creating an API Key

In order to use the enabled APIs, you will need to generate an API key for your Android app. On the credentials page for your Google project, select one of your enabled APIs from the top dropdown menu, and Android from the second.

Google API Console Add credentials to your project

Next you will be taken to a screen where you can enter a package name for your app and the SHA1 for the app's signing key. In order to get the signing key SHA1 for your debug key on Linux or OS X, enter the following command in a terminal window.

On Windows you can run the same command with the path set to the location of your debug.keystore file.

Terminal window for creating credentials

Once you have your SHA1 key, enter it into the Google API credential creator.

Google API Console Create an API Key

Once you click the Create API key button, you will be given the API key that you will need to use in your Android application.

Google API Console API Key

Conclusion

In this article you have learned about what Google Play Services is and what features it can provide to your Android applications. In addition, you have been introduced to Firebase, a powerful tool providing many excellent services for your app, and have seen how to create an API key for accessing Google Play Services in your apps. 

Tags:

Comments

Related Articles