使用数据API的变化来更新表盘UI [英] Using Data API changes to update watch face UI

查看:164
本文介绍了使用数据API的变化来更新表盘UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用中,从一个手机应用程序在云拉低数据,并同步其通过数据映射到配对的手表应用一个表盘Android Wear服务数据API。但我如何得到的耐磨侧业务能够正常收听更新和重绘其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 I'm a bit lost on how to get the service on the wearable side to properly listen to updates AND redraw its UI. Should I be doing this all from the same service?

我想实现在扩展CanvasWatchFaceService.Engine私有内部类的DataApi.DataListener方法,但似乎并没有工作。

I tried implementing the DataApi.DataListener methods in the private internal class that extends CanvasWatchFaceService.Engine, but that doesn't seem to work.

@Override
    public Engine onCreateEngine() {
        return new Engine();
    }

    private class Engine extends CanvasWatchFaceService.Engine implements DataApi.DataListener {

       // other events and methods...    

        @Override
        public void onDataChanged(DataEventBuffer dataEvents) {
            for(DataEvent event : dataEvents) {
                DataItem dataItem = event.getDataItem();
                if(PATH.equals(dataItem.getUri().getPath())) {
                    DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
                    downloadedTrends = dataMap.getString(DATAMAP_KEY);
                }
            }
        }

       @Override
       public void onDraw(Canvas canvas, Rect bounds) {
          // use the changed data here to update the watch face UI
       }
    }

我应该使用扩展WearableListenerService一个单独的服务,如果是这样,我怎么提取的onDraw数据在表盘服务?我可以直接挖掘DataEventBuffer回调之外吗?

Should I be using a separate service extending WearableListenerService, and if so, how do I extract the data in onDraw in the watch face service? Can I tap the DataEventBuffer directly outside of the callback?

我也认为这可能是与清单中的BIND_LISTENER意图过滤器,因为我实现它的私有内部类。

I'm also think it might have something to do with the BIND_LISTENER intent filter in the manifest, since I'm implementing it on the private internal class.

感谢您的帮助!

推荐答案

您应该这两项服务分开。表盘服务吸引表盘,数据API服务处理数据项的变化。

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

现在,你需要以某种方式将它们连接起来。有几种方法。我将与踏实,很丑陋的开始:你的数据API服务有一个公共的,静态的界面,通过它,其他组件可以注册听众,并获得有关数据变化的信息。只要你在同一个过程中,一切都很好。当你的手表表面引擎是创造,它本身注册为一个监听器,当它被破坏,将注销本身。

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.

有替代品。您的数据的API服务可以发送广播,当它接收到一个新数据项和表盘可以听他们。它可以包括在数据中,也可以具有通过数据项支持的内容提供者(在此情况下,内容提供者将来自数据API服务接收广播和表盘会对那个提供者的内容的观察者)。

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?

这篇关于使用数据API的变化来更新表盘UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆