Introduction
Android O introduced the Awareness API, a powerful tool for creating contextual experiences based on user’s environment. This API leverages various sensors and data sources to provide insights like location, weather, activity, and more. BroadcastReceiver plays a crucial role in receiving real-time updates from the Awareness API.
Understanding Awareness API
What is Awareness API?
The Awareness API provides access to contextual information about the user’s environment and behavior. It offers a set of APIs to obtain data like:
- Location: Current location and recent movement history
- Weather: Current weather conditions
- Activity: User’s activity, such as walking, running, or driving
- Time: Current time and date
- Place: User’s current place, like home, work, or a specific location
How does Awareness API work?
The Awareness API uses a combination of sensors, Google’s location services, and other data sources to collect information. It utilizes a background service to constantly monitor and update the data. This data is then accessible through the API.
Using BroadcastReceiver with Awareness API
Creating a BroadcastReceiver
A BroadcastReceiver is a component that listens for system-wide events. To receive updates from the Awareness API, we need to create a BroadcastReceiver that listens to specific intents.
public class AwarenessReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Get the Awareness data from the intent AwarenessData awarenessData = intent.getParcelableExtra(Awareness.EXTRA_DATA); // Process the data based on the intent action if (intent.getAction().equals(Awareness.ACTION_AWARENESS_API_UPDATE)) { // Handle awareness data updates // Example: // Location location = awarenessData.getLocation(); // ... } else if (intent.getAction().equals(Awareness.ACTION_AWARENESS_API_ERROR)) { // Handle errors in retrieving awareness data } } }
Registering the BroadcastReceiver
We need to register the BroadcastReceiver in the AndroidManifest.xml file to make it active and receive updates from the Awareness API.
<receiver android:name=".AwarenessReceiver"> <intent-filter> <action android:name="com.google.android.awareness.ACTION_AWARENESS_API_UPDATE" /> <action android:name="com.google.android.awareness.ACTION_AWARENESS_API_ERROR" /> </intent-filter> </receiver>
Requesting Awareness Updates
We can request specific awareness data by using the Awareness API methods.
// Request location updates Awareness.SnapshotApi.getLocation(context) .addOnSuccessListener(location -> { // Handle location data }) .addOnFailureListener(exception -> { // Handle errors }); // Request weather updates Awareness.SnapshotApi.getWeather(context) .addOnSuccessListener(weather -> { // Handle weather data }) .addOnFailureListener(exception -> { // Handle errors });
Example: Displaying User’s Current Location
This example demonstrates how to display the user’s current location using Awareness API and a BroadcastReceiver.
Code
package com.example.awarenessapp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.location.Location; import android.widget.TextView; import com.google.android.gms.awareness.Awareness; import com.google.android.gms.awareness.snapshot.LocationResult; public class AwarenessReceiver extends BroadcastReceiver { private TextView locationTextView; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Awareness.ACTION_AWARENESS_API_UPDATE)) { LocationResult locationResult = intent.getParcelableExtra(Awareness.EXTRA_DATA); Location location = locationResult.getLocation(); if (location != null) { locationTextView.setText("Current Location: " + location.getLatitude() + ", " + location.getLongitude()); } } } // ... Other methods for setting locationTextView ... }
Output
The output will display the user’s current latitude and longitude in the TextView.
Current Location: 40.7128, -74.0060
Conclusion
The Awareness API is a powerful tool for developers who want to create context-aware Android applications. Using BroadcastReceiver, we can efficiently receive real-time updates from the Awareness API, providing users with personalized and engaging experiences based on their surroundings.