Using Data API changes to update watch face UI

前端 未结 3 837
挽巷
挽巷 2021-01-14 22:44

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 a

3条回答
  •  无人及你
    2021-01-14 23:13

    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.

    public class DataLayerService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        SettingsManager sm = new SettingsManager(getApplicationContext()); 
     //This is an interface for SharedPreferences I built for convenience
     DataMap dm = DataMap.fromByteArray(d.getDataItem().getData());
     sm.setString("WP_ICON", dm.getString("Icon"));
    }
    }
    

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

    private class Engine extends CanvasWatchFaceService.Engine {
        @Override
        public void onDraw(Canvas canvas, Rect bounds) {
            Drawable weather = 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.

提交回复
热议问题