Awareness API 的最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Awareness API 可让您对用户情境的变化做出响应,但拥有这项强大功能的同时,也需要承担相应责任。为了为用户提供最佳体验,请考虑以下准则。
- 注意用户的期望
- 确保用户了解您的应用会提供哪些功能。例如,假设您的应用旨在让用户插入耳机后立即播放音乐。如果用户不希望出现此类行为,这可能会导致糟糕的体验。
- 谨慎使用通知
- 如果您的应用会发送通知,请向用户明确表明这一点。仅发送与用户相关的通知。请注意,您的应用必须与许多可能也会发送通知的其他应用共存。
- 维护系统健康
- 虽然我们投入了大量精力来提高 Awareness API 的效率,但如果使用不当,它仍有可能耗用大量电池电量。Awareness API 可为您管理系统运行状况。不过,如果您注册的屏幕边界过多,Awareness API 会节流回调,以防止电池电量过度耗尽。同样,过多的快照请求也会耗尽电池电量。
- 使用 Awareness API 了解认知度
- 如果您的应用需要执行非实时任务(例如数据同步或清理操作),请考虑使用 JobScheduler API 执行此类任务。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe Awareness API lets your app react to user context changes, but it's crucial to use it responsibly to avoid negative user experiences.\u003c/p\u003e\n"],["\u003cp\u003eManage user expectations by clearly indicating how your app will utilize the Awareness API and what actions it might trigger.\u003c/p\u003e\n"],["\u003cp\u003eMinimize notifications and ensure they are relevant to the user to prevent notification overload and maintain a positive user experience.\u003c/p\u003e\n"],["\u003cp\u003eBe aware of the Awareness API's potential impact on battery life and avoid excessive fence registrations or snapshot requests.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the Awareness API primarily for context-aware features and consider alternatives like JobScheduler for non-time-sensitive tasks.\u003c/p\u003e\n"]]],[],null,["# Best practices for the Awareness API\n\nThe Awareness API gives you the power to react to changes in a user's context,\nbut with this great power comes great responsibility. In order to provide an\noptimal experience for your users, consider the following guidelines.\n\nBe mindful of user expectations\n: Be sure the user understands what to expect from your app. For example, suppose\n your app is designed to instantly play music when the user plugs in their\n headphones. This could result in a bad experience if the user doesn't expect\n such behavior.\n\nBe conservative with notifications\n: If your app sends notifications, clearly communicate this to the user.\n Send only notifications that are relevant to the user. Keep in mind that your\n app must coexist with many other apps that might also send notifications.\n\nConserve system health\n: Although great effort was spent to make the Awareness API efficient,\n it still has the potential to consume significant amounts of battery life if\n used carelessly. The Awareness API manages system health for you. However, if you\n register too many fences, the Awareness API throttles callbacks to prevent\n undue battery drain. Likewise, excessive snapshot requests can also\n drain battery life.\n\nUse the Awareness API for awareness\n: If your app needs to perform tasks that aren't time-sensitive, such as data\n synchronization or cleanup operations, consider the\n [JobScheduler API](http://developer.android.com/reference/android/app/job/JobScheduler)\n to perform such tasks."]]