什么是 Awareness API?
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 Google Awareness API,您可以让应用智能地对用户的当前情况做出响应。Awareness API 提供五种不同的情境类型,包括用户活动和附近信标。这些类型可让您的应用以前所未有的全新方式提升用户体验。您的应用可以结合这些上下文信号来推断用户当前所处的情境,并利用这些信息提供个性化体验,例如在用户插入耳机并开始慢跑时显示播放列表建议。
Awareness API 的优势
Awareness API 具有以下几点优势:
- 易于实现:您只需向应用添加一个 API,这大大简化了集成流程并提高了工作效率。
- 更实用的情境数据:系统会处理原始信号,以提升质量。例如,系统会使用先进的算法以高精度确定用户的活动。
- 最佳系统运行状况:Awareness API 会自动管理对电池续航时间和数据用量的影响,这样您的应用就不必自行管理。
围栏和快照
Awareness API 由两个不同的 API 组成,您的应用可以使用这两个 API 获取情境信号,以便确定用户的当前情况:
- Fence API:借助此 API,您的应用可以对用户的当前情况做出响应,并在满足组合情境条件时提供通知。例如,每当用户外出散步并插入耳机时。注册栅栏后,即使应用未运行,Fence API 也可以向应用发送回调。
- Snapshot API:借助此 API,您的应用可以请求有关用户当前情境的信息,例如用户的当前位置和当前天气状况。
上下文类型
情境是 Awareness API 的核心。情境数据包括传感器派生的数据,例如位置(纬度和经度)、地点类型(例如公园或咖啡馆)和活动(例如步行或驾车)。这些基本类型(或信号)可以组合使用,以更具体地推断用户的情况。展开即可下通知,查看哪些情境信号已被弃用。
下表介绍了 Awareness API 目前提供的基本情境类型:
上下文类型 |
示例 |
时间 |
当前当地时间 |
位置 |
经纬坐标 |
活动 |
检测到的用户活动,例如步行、跑步或骑车 |
信标 |
与指定命名空间匹配的附近信标 |
耳机 |
耳机是否已插入的状态 |
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe Google Awareness API lets your app intelligently respond to the user's context, such as their activity, location, and nearby beacons.\u003c/p\u003e\n"],["\u003cp\u003eIt offers benefits like easy implementation, improved context data quality, and automatic management of system resources.\u003c/p\u003e\n"],["\u003cp\u003eThe API provides two ways to access context: the Fence API for reacting to situations and the Snapshot API for requesting current information.\u003c/p\u003e\n"],["\u003cp\u003eSupported context types include time, location, activity, beacons, and headphone status, while places and weather signals are deprecated.\u003c/p\u003e\n"]]],[],null,["# What's the Awareness API?\n\nWith the Google Awareness API, you can enable your app to intelligently react to the\nuser's current situation. The Awareness API exposes five different\n[context types](#context-types), which include user activity,\nand nearby beacons. These types enable your app to refine the user experience in\nnew ways that weren't possible before. Your app can combine these context\nsignals to make inferences about the user's current situation, and use this\ninformation to provide customized experiences, such as a playlist suggestion\nwhen the user plugs in their headphones and starts to jog.\n\nAwareness API benefits\n----------------------\n\nThe Awareness API provides several benefits:\n\n- **Ease of implementation:** You only need to add a single API to your app, which greatly simplifies integration and improves your productivity.\n- **Better context data:** Raw signals are processed for improved quality. For example, advanced algorithms are used to determine the user's activity with a high level of accuracy.\n- **Optimal system health:** The Awareness API automatically manages its impact on battery life and data usage so that your app doesn't have to.\n\nFences and snapshots\n--------------------\n\nThe Awareness API consists of two distinct APIs that your app can use to\nget context signals in order to determine the user's current situation:\n\n- [**Fence API:**](/awareness/android-api/fence-api-overview) This API lets your app react to the user's current situation, and provides notification when a combination of context conditions are met. For example, whenever the user takes a walk and their headphones are plugged in. Once a fence is registered, the Fence API can send callbacks to your app even when it's not running.\n- [**Snapshot API:**](/awareness/android-api/snapshot-api-overview) This API lets your app request information about the user's current context, such as the user's current location and the current weather conditions.\n\nContext types\n-------------\n\nContext is at the heart of the Awareness API. Contextual data includes\nsensor-derived data such as location (latitude and longitude), place type,\nlike a park or coffee shop, and activity, such as a walk or drive. These basic\ntypes, or signals, can be combined to extrapolate the user's situation in more\nspecific detail. Expand the following notice to see which contextual signals\nhave been deprecated.\n**Deprecation notice: Places and Weather contextual signals** **Warning:**\n| The Places and Weather contextual signals, exposed by the\n| [`getPlaces()`](/android/reference/com/google/android/gms/awareness/SnapshotClient#getPlaces()), and\n| [`getWeather()`](/android/reference/com/google/android/gms/awareness/SnapshotClient#getWeather())\n| methods, were deprecated.\n|\n| - The Places contextual signal was turned off. Continued use of this signal was restricted to customers with existing implementations, through October 30, 2019. New implementations must use the [Places SDK for Android](/places/android-sdk) instead of the Places contextual signal.\n| - The Weather contextual signal has also been turned off. Continued use of this signal has been restricted to customers with existing implementations, through January 31, 2020. Google doesn't offer alternative functionality for the Weather contextual signal.\n\nThe following table describes the basic context types currently offered by\nthe Awareness API:\n\n| Context type | Example |\n|--------------|----------------------------------------------------------|\n| Time | Current local time |\n| Location | Latitude and longitude |\n| Activity | Detected user activity, like walking, running, or biking |\n| Beacons | Nearby beacons that match the specified namespace |\n| Headphones | Status of whether headphones are plugged in, or not |"]]