In this tutorial you will learn about the Awareness API and how to access snapshots of data, as well as how to create listeners (known as fences, taking their name from geofences) for combinations of user conditions that match the goals of your applications. This can be useful for a wide variety of apps, such as location-based games, offering coupons to users in stores, and starting a music app when you detect a user exercising. All code for this sample application can be found on GitHub.
1. Setting Up the Developer Console
Before diving into your Android application, you will need to set up Google Play Services through the Google API Console. If you already have a project created, you can skip the first step of this section. If not, you can click the above link and follow along to create a new project for your application.
Step 1: Creating a Project
To create a new project, click on the blue Create Project button in the top center of the screen.
![]() |
![]() |
![]() |
From the Google APIs Overview screen, select the search box and search for the Awareness API.
![]() |
![]() |
![]() |
Step 3: Creating An Android 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 Awareness API from the top dropdown menu and Android from the second.
![]() |
- keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
![]() |
![]() |
Once you have an API key created and the proper APIs enabled, it's time to start setting up your Android project. For this tutorial, we'll create a test application in order to learn the API.
In order to demonstrate all of the features of the Awareness API, this tutorial will focus on using a simple list representing each feature used. Although the details of creating this list will not be discussed, you can find a complete implementation in the GitHub source for this tutorial.
Step 1: Set Up Play Services
First you will need to include the Play Services library in your build.gradle file. While you can include all of Google Play Services, it's best to include only the packages you need for your app.
In this case, the Awareness API is available in the ContextManager package, and it can be included in your project by adding the following line within your dependencies node. You will also want to make sure the AppCompat library is included, as this will be used for checking permissions on Marshmallow and above devices.
- compile 'com.google.android.gms:play-services-contextmanager:9.2.0'
- compile 'com.android.support:appcompat-v7:23.4.0'
- <string name="google_play_services_key">YOUR API KEY HERE</string>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
- <meta-data
- android:name="com.google.android.awareness.API_KEY"
- android:value="@string/google_play_services_key" />
- <!-- places/location declaration -->
- <meta-data
- android:name="com.google.android.geo.API_KEY"
- android:value="@string/google_play_services_key" />
- <!-- Beacon snapshots/fences declaration -->
- <meta-data
- android:name="com.google.android.nearby.messages.API_KEY"
- android:value="@string/google_play_services_key" />
- public class MainActivity extends AppCompatActivity implements
- GoogleApiClient.OnConnectionFailedListener {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addApi(Awareness.API)
- .enableAutoManage(this, this)
- .build();
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
- }
Now that Play Services are configured within your Android app, you will need to ensure that your users on Android Marshmallow or higher have granted permission for your application to use their location. You can check for this permission in onCreate(Bundle) and before you access any features that require the location permission in order to avoid crashes within your app.
- private boolean checkLocationPermission() {
- if( !hasLocationPermission() ) {
- Log.e("Tuts+", "Does not have location permission granted");
- requestLocationPermission();
- return false;
- }
- return true;
- }
- private boolean hasLocationPermission() {
- return ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION )
- == PackageManager.PERMISSION_GRANTED;
- }
- private final static int REQUEST_PERMISSION_RESULT_CODE = 42;
- private void requestLocationPermission() {
- ActivityCompat.requestPermissions(
- MainActivity.this,
- new String[]{ Manifest.permission.ACCESS_FINE_LOCATION },
- REQUEST_PERMISSION_RESULT_CODE );
- }
![]() |
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
- @NonNull int[] grantResults) {
- switch (requestCode) {
- case REQUEST_PERMISSION_RESULT_CODE: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- //granted
- } else {
- Log.e("Tuts+", "Location permission denied.");
- }
- }
- }
- }
Written by Paul Trebilcox-Ruiz
If you found this post interesting, follow and support us.
Suggest for you:
Android Application Programming - Build 20+ Android Apps
The Complete Android Developer Course: Beginner To Advanced!
Android: From Beginner to Paid Professional
The Complete Android Developer Course - Build 14 Apps
Python For Android Hacking Crash Course: Trojan Perspective
No comments:
Post a Comment