Skip to content Skip to sidebar Skip to footer

Using Data Api Changes To Update Watch Face Ui

I'm using the Data API in a service for Android Wear in a watch face that pulls down data from the cloud in a phone app and syncs it through a DataMap to the paired watch app. But

Solution 1:

You should separate these two services. Watch face services draws the watch face, data api service handles data item changes.

Now, you need to somehow connect them. There are several ways. I will start with the practical, very ugly one: your data api service has a public, static interface, through which other components can register "listeners" and get information about data changes. As long as you are in the same process, everything is fine. When your watch face engine is create, it registers itself as a listener and when it's destroyed, it deregisters itself.

There are alternatives. Your data api service can send broadcasts, when it receives a new data item and the watch face can listen for them. It can either include the data inside or you can have a content provider backed by data items (in this case, the content provider would receive a broadcast from the data api service and the watch face would have a content observer on that provider).

Does any of these approaches suit your needs?

Solution 2:

While having separate service could definitely fit the bill, I was researching for possibly having just one service for both CanvasWatchFaceService and being registered for Data Layers API, and seems the DigitalWatchFaceService in the Google sample/android-WatchFace provide an example of this.

Notice it provide example to register both as BroadcastReceiver AND the Data API.

Hope this helps.

https://github.com/googlesamples/android-WatchFace/blob/master/Wearable/src/main/java/com/example/android/wearable/watchface/DigitalWatchFaceService.java

Solution 3:

I used a separate service to listen for dataitem changes. From there, I used SharedPreferences to store the data and recall it in my Draw method.

publicclassDataLayerServiceextendsWearableListenerServiceimplementsGoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
@OverridepublicvoidonDataChanged(DataEventBuffer dataEvents) {
    SettingsManagersm=newSettingsManager(getApplicationContext()); 
 //This is an interface for SharedPreferences I built for convenienceDataMapdm= DataMap.fromByteArray(d.getDataItem().getData());
 sm.setString("WP_ICON", dm.getString("Icon"));
}
}

This code basically gets data from a phone and saves it locally.

privateclassEngineextendsCanvasWatchFaceService.Engine {
    @OverridepublicvoidonDraw(Canvas canvas, Rect bounds) {
        Drawableweather= getDrawable(getIcon(sm.getString("WP_ICON")));
        //The getIcon method chooses a Drawable based on the string. //The values are from a set list
   }
}

It's a basic overview of how I did this from the watch side. This works pretty well functionality wise. Without a paired device it won't have any data issues as it's already offline.

Post a Comment for "Using Data Api Changes To Update Watch Face Ui"