Ankhwatcher
Ankhwatcher

Reputation: 420

Determine when my Watch Face is being displayed

I've built a couple of Android Wear watch faces and I'd like to use Google Analytics to track when they are set and un-set so that I have an idea of how many active users I have.

I know in order to use Google Analytics from Android Wear I have to send messages to a receiver in the mobile app so it can communicate to GA for me, but I'm not sure where in the lifecycle of the watch faces I should send these messages.

Thanks!

Upvotes: 0

Views: 138

Answers (1)

x90
x90

Reputation: 2170

You can implement WearableListenerService or DataApi.DataListener in your activity on mobile app side. Then in your watchface WatchFaceService.Engine implementation in OnCreate method you can connect to google api client:

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

then when you connected to google api client send dataitem to your mobile app. it will indicate that service was launched:

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

then in onDestroy method of your WatchFaceService.Engine class you can send info about watchface life end to your mobile app in the way describet abowe(via dataitem). And on side of mobile app handle this data in WearableListenerService and put it to GA. For more details check docs.

Upvotes: 1

Related Questions