Android 開發(fā)者需要知道的 Google Awareness API

Google 在 I/O 2016 大會上正式向開發(fā)者介紹了 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 中和用戶場景識別相關(guān)的服務(wù)和功能整合在一個統(tǒng)一的 API 下,為開發(fā)者從兼顧內(nèi)存占用和電量消耗方面提供更高效率的方案。

Google 定義的場景識別

Google 在 Awareness API 的文檔中,對智能化所需的場景是 這樣描述 的:

具體來說:

  • 時間:系統(tǒng)當(dāng)?shù)貢r間。
  • 地點:當(dāng)前用戶位置,該位置的區(qū)域類別信息。
  • 行為:用戶正在做什么事。
  • 近場 Beacons:附近有哪些 Beacon 設(shè)備。
  • 設(shè)備狀態(tài):耳機是否已經(jīng)接入設(shè)備等。
  • 環(huán)境信息:光照、氣壓等。

為了提供這些數(shù)據(jù)采集能力,Awareness API 提供了兩種類型的服務(wù):

  • Fence API:可對用戶周圍的環(huán)境變化做出反應(yīng)。Fence API 可以將多種場景條件結(jié)合在一起,當(dāng)這些條件滿足時通過廣播通知用戶,及時此時訂閱了該廣播的應(yīng)用處于后臺。
  • Snapshot API:通過一個簡單的接口獲取由 7 種信號描述的用戶當(dāng)前的場景信息。Google 通過智能緩存和跨應(yīng)用響應(yīng)改善了電量消耗和內(nèi)存占用。

相比直接通過系統(tǒng)接口采集相關(guān)傳感器數(shù)據(jù),使用 Awareness API 有哪些優(yōu)勢呢,Google 提到

  • 更易集成:只要引入 Google Play Service,通過 Awareness API 一個接口,就可以獲得所有場景相關(guān)數(shù)據(jù),提高集成效率和生產(chǎn)率。
  • 更高質(zhì)量的場景數(shù)據(jù):Awareness API 返回的數(shù)據(jù)是 Google 通過專門優(yōu)化算法對原始傳感器數(shù)據(jù)處理后的結(jié)果,質(zhì)量更好,更適合于相關(guān)業(yè)務(wù)開發(fā)。
  • 更好的電池和內(nèi)存效率:Google Play Service 在后臺做數(shù)據(jù)處理,所有安裝的應(yīng)用都可以根據(jù)需要獲取這些數(shù)據(jù),無需自己通過直接的傳感器數(shù)據(jù)采集實現(xiàn)場景識別,節(jié)省大量電量和內(nèi)存的消耗。

Awareness API 的集成

Awareness API 的集成文檔 為開發(fā)者提供了詳細(xì)的集成步驟和一些 Best Practices。

申請使用 Awareness API 的 AppKey 以及相應(yīng)功能的 Credentials

為了在應(yīng)用中集成 Awareness API, 我們首先需要登錄 developer console,創(chuàng)建一個新的 Project:

選擇剛剛創(chuàng)建的工程 AwarenessDemo,進(jìn)入 API Manager,添加相關(guān)權(quán)限:

然后,提供應(yīng)用的 Package Name 和用來對應(yīng)用進(jìn)行簽名的證書 SHA1 指紋生成 APIKey:

That's it! 這樣就生成了一個可以調(diào)用 Awareness API 的 key,我們后面將在 AndroidManifest.xml 中對其進(jìn)行配置。

應(yīng)用集成配置

  • 我們需要使用 SDK Manager 將 Google play Service 更新到最新版本,也就是 9.2.1 版本以上。

  • 在 Android Studio 中創(chuàng)建一個新的 Android 應(yīng)用,該應(yīng)用的 Package Name 必須和上面生成 APIKey 時使用到的包名相同。

  • 在應(yīng)用的 build.gradle 中添加對 Google Play Service 的依賴:

      dependencies {
          ...
          compile 'com.google.android.gms:play-services-contextmanager:9.2.1'
      }
    
  • AndroidManifest.xml 文件中添加 Awareness API 所需的權(quán)限:

      <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。

代碼集成

  • 在應(yīng)用自定義的 Application 類中通過下面的方式進(jìn)行 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 提供的場景數(shù)據(jù)時,就可以直接調(diào)用 com.google.android.gms.awareness.Awareness 類了。比如實時識別的用戶行為數(shù)據(jù),可以通過下面的代碼獲?。?/p>

      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());
                  }
              });
    

當(dāng)這里為止,我們已經(jīng)成功集成了 Awareness API。需要注意的是,對于 Awareness API 可以提供的有些數(shù)據(jù),需要 Enable 相應(yīng)的 API,在 Library 中搜索啟用即可:

比如用戶周圍的 POI 數(shù)據(jù),查找 place API 并啟用就可以了。

Best Practices

由于 Awareness API 提供了較為隱私的用戶數(shù)據(jù),所以我們在應(yīng)用中使用該 API 時,需要注意不要濫用,管理好用戶的期望。Google 提出了下面的最佳實踐建議:

  • Be mindful of user expectations,也就是說考慮這些功能和應(yīng)用提供服務(wù)的統(tǒng)一性,不要設(shè)計和實現(xiàn)容易導(dǎo)致用戶疑惑和恐慌的功能。
  • Be conservative with notifications,管理好通知,不必要不要打擾用戶,提供友好的交互。
  • Conserve system health,注意不要涉及過于復(fù)雜的場景,避免電量和內(nèi)存的過度消耗。
  • 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 的一個開發(fā)者也在 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.

個性化的智能服務(wù)不可避免地需要用到用戶的部分隱私數(shù)據(jù),作為用戶,為了獲得更智能的服務(wù)和體驗,也需要出讓部分隱私數(shù)據(jù)。作為負(fù)責(zé)任的開發(fā)者,我們需要精心設(shè)計應(yīng)用的功能,為用戶提供這些服務(wù),同時做到不打擾用戶,不讓用戶感到不適,當(dāng)然,最重要的是保護(hù)好用戶的數(shù)據(jù)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容