2015-04-06 95 views
0

我已經構建了一些Android Wear手錶,並且我想使用Google Analytics(分析)來跟蹤它們何時設置和取消設置,以便我瞭解有多少我有活躍用戶。確定我的手錶正在顯示的時間

我知道爲了使用Android Wear的Google Analytics我必須將消息發送到移動應用程序中的接收器,以便它可以與GA溝通,但我不確定手錶的生命週期中的哪個部分我應該發送這些消息。

謝謝!

回答

1

您可以在移動應用程序端的活動中實現WearableListenerService或DataApi.DataListener。然後在onCreate方法的錶盤WatchFaceService.Engine實現,你可以連接到Google API客戶端:

 GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(SocialWatchFace.this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(Wearable.API) 
       .build(); 
mGoogleApiClient.connect() 

那麼當你連接到Google的API客戶端發送的DataItem到您的移動應用程序。這將表明,服務推出:

@Override 
public void onConnected(Bundle bundle) { 
    PutDataMapRequest putDataMapReq = PutDataMapRequest.create("your.app.package/path"); 
    DataMap dataMap = putDataMapReq.getDataMap(); 
    //put your info to map 
    PutDataRequest putDataReq = putDataMapReq.asPutDataRequest(); 
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq); 
} 

然後在WatchFaceService.Engine類的onDestroy方法,你可以在路上describet abowe(通過DataItem的)發送關於錶盤報廢信息到移動應用。 而移動應用程序在WearableListenerService中處理這些數據並將其放到GA中。 更多詳情請查看docs

+1

您可能希望將其移入單獨的服務中,因此如果您的錶盤面被改變,您就不會使用GoogleApiClient。相反,一個單獨的服務可以爲您處理工作,並在完成時自行完成。 – gruszczy