Google 在 I/O 2016 大會上正式向開發者介紹了 Awareness API:
A unified sensing platform enabling applications to be aware of multiple aspects of a users context, while managing battery and memory health.
Google 將 Google Play Service 中和用戶場景識別相關的服務和功能整合在一個統一的 API 下,為開發者從兼顧內存占用和電量消耗方面提供更高效率的方案。
Google 定義的場景識別
Google 在 Awareness API 的文檔中,對智能化所需的場景是 這樣描述 的:
具體來說:
- 時間:系統當地時間。
- 地點:當前用戶位置,該位置的區域類別信息。
- 行為:用戶正在做什么事。
- 近場 Beacons:附近有哪些 Beacon 設備。
- 設備狀態:耳機是否已經接入設備等。
- 環境信息:光照、氣壓等。
為了提供這些數據采集能力,Awareness API 提供了兩種類型的服務:
- Fence API:可對用戶周圍的環境變化做出反應。Fence API 可以將多種場景條件結合在一起,當這些條件滿足時通過廣播通知用戶,及時此時訂閱了該廣播的應用處于后臺。
- Snapshot API:通過一個簡單的接口獲取由 7 種信號描述的用戶當前的場景信息。Google 通過智能緩存和跨應用響應改善了電量消耗和內存占用。
相比直接通過系統接口采集相關傳感器數據,使用 Awareness API 有哪些優勢呢,Google 提到:
- 更易集成:只要引入 Google Play Service,通過 Awareness API 一個接口,就可以獲得所有場景相關數據,提高集成效率和生產率。
- 更高質量的場景數據:Awareness API 返回的數據是 Google 通過專門優化算法對原始傳感器數據處理后的結果,質量更好,更適合于相關業務開發。
- 更好的電池和內存效率:Google Play Service 在后臺做數據處理,所有安裝的應用都可以根據需要獲取這些數據,無需自己通過直接的傳感器數據采集實現場景識別,節省大量電量和內存的消耗。
Awareness API 的集成
Awareness API 的集成文檔 為開發者提供了詳細的集成步驟和一些 Best Practices。
申請使用 Awareness API 的 AppKey
以及相應功能的 Credentials
為了在應用中集成 Awareness API, 我們首先需要登錄 developer console,創建一個新的 Project:
選擇剛剛創建的工程 AwarenessDemo
,進入 API Manager,添加相關權限:
然后,提供應用的 Package Name
和用來對應用進行簽名的證書 SHA1
指紋生成 APIKey
:
That's it! 這樣就生成了一個可以調用 Awareness API 的 key,我們后面將在 AndroidManifest.xml
中對其進行配置。
應用集成配置
我們需要使用 SDK Manager 將 Google play Service 更新到最新版本,也就是 9.2.1 版本以上。
在 Android Studio 中創建一個新的 Android 應用,該應用的
Package Name
必須和上面生成APIKey
時使用到的包名相同。-
在應用的
build.gradle
中添加對 Google Play Service 的依賴:dependencies { ... compile 'com.google.android.gms:play-services-contextmanager:9.2.1' }
-
在
AndroidManifest.xml
文件中添加 Awareness API 所需的權限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
-
同樣在
AndroidManifest.xml
文件中,添加APIKey
的配置:<application> <meta-data android:name="com.google.android.awareness.API_KEY" android:value="YOUR_API_KEY" /> <meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY" /> <meta-data android:name="com.google.android.nearby.messages.API_KEY" android:value="YOUR_API_KEY" /> ... </application>
用剛才生成的
APIKey
替換YOUR_API_KEY
。
代碼集成
-
在應用自定義的
Application
類中通過下面的方式進行GoogleApiClient
的初始化:private GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this) .addApi(Awareness.API) .build(); mGoogleApiClient.connect(); // 其他代碼 }
-
GoogleApiClient
初始化后,在我們需要獲取 Awareness API 提供的場景數據時,就可以直接調用com.google.android.gms.awareness.Awareness
類了。比如實時識別的用戶行為數據,可以通過下面的代碼獲取:Awareness.SnapshotApi.getDetectedActivity(mGoogleApiClient) .setResultCallback(new ResultCallback<DetectedActivityResult>() { @Override public void onResult(@NonNull DetectedActivityResult detectedActivityResult) { if (!detectedActivityResult.getStatus().isSuccess()) { Log.e(TAG, "Could not detect user activity"); mUserActivityTextView.setText("--Could not detect user activity--"); mUserActivityTextView.setTextColor(Color.RED); return; } ActivityRecognitionResult arResult = detectedActivityResult.getActivityRecognitionResult(); DetectedActivity probableActivity = arResult.getMostProbableActivity(); Log.i(TAG, probableActivity.toString()); } });
當這里為止,我們已經成功集成了 Awareness API。需要注意的是,對于 Awareness API 可以提供的有些數據,需要 Enable
相應的 API,在 Library
中搜索啟用即可:
比如用戶周圍的 POI 數據,查找 place
API 并啟用就可以了。
Best Practices
由于 Awareness API 提供了較為隱私的用戶數據,所以我們在應用中使用該 API 時,需要注意不要濫用,管理好用戶的期望。Google 提出了下面的最佳實踐建議:
- Be mindful of user expectations,也就是說考慮這些功能和應用提供服務的統一性,不要設計和實現容易導致用戶疑惑和恐慌的功能。
- Be conservative with notifications,管理好通知,不必要不要打擾用戶,提供友好的交互。
- Conserve system health,注意不要涉及過于復雜的場景,避免電量和內存的過度消耗。
- Use the Awareness API for awareness,僅在需要的時候使用 Awareness API。
正如對 Awareness API 同樣感興趣,并且寫了一篇 非常好的體驗文章 的作者提到的:
Be aware that while some uses of the Awareness API can make your app feel smart, delightful and intuitive, too much use, or usage in an unexpected way can have the opposite effect and make your app users uncomfortable, and your app feel creepy. Remember, similar to great power, with great APIs, comes great responsibility. Use at your own discretion.
Google Android Team 的一個開發者也在 Using the Awareness API for Android 文章中提到:
Always explain how you’re using their context, and what you’re doing with the data?—?both on the device, and especially if that data is being stored or transmitted.
Don’t transmit or store location or contact details unless that’s clear to the user, and a critical part of your app’s functionality.
Have a clear privacy policy that’s easy for users to find and understand.
If you are storing any context data, make it simple and easy for users to erase it?—?both on their device and on your servers.
個性化的智能服務不可避免地需要用到用戶的部分隱私數據,作為用戶,為了獲得更智能的服務和體驗,也需要出讓部分隱私數據。作為負責任的開發者,我們需要精心設計應用的功能,為用戶提供這些服務,同時做到不打擾用戶,不讓用戶感到不適,當然,最重要的是保護好用戶的數據。