Android的磨损Watchface设置主机 [英] Android Wear Watchface Settings on host

查看:165
本文介绍了Android的磨损Watchface设置主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我已经开发了一个机器人的磨损watchface。然而,我现在想创建主机应用程序中的设置部分,将允许用户自定义watchface。我是新来的android开发,所以我很好奇在这样做的正确方法。

I currently have an android wear watchface developed. I however would now like to create a settings section on the host app that would allow the user to customize the watchface. I am new to the android development so I am curious on the correct way to do this.

有没有一种方法来更新主机上的共享preference然后推或同步,与磨损设备上的共享preference?或者是有一个完全不同的方式,我应该看这个?

Is there a way to update a sharedpreference on the host and then push or sync that with the sharedpreference on the wear device? Or is there a totally different way I should be looking at this?

推荐答案

您可以使用 DataApi MessageApi 同步手机和观看设备之间的wat​​chface配置。

You can use the DataApi or MessageApi to sync your watchface configuration between Phone and Watch devices.

请看一看的文件,并选择一个更适合您的需求:
<一href="https://developer.android.com/training/wearables/data-layer/index.html">https://developer.android.com/training/wearables/data-layer/index.html <一href="https://developer.android.com/training/wearables/data-layer/data-items.html">https://developer.android.com/training/wearables/data-layer/data-items.html
<一href="https://developer.android.com/training/wearables/data-layer/messages.html">https://developer.android.com/training/wearables/data-layer/messages.html

Please take a look at the documentation and choose the one more appropriate to your needs:
https://developer.android.com/training/wearables/data-layer/index.html https://developer.android.com/training/wearables/data-layer/data-items.html
https://developer.android.com/training/wearables/data-layer/messages.html

下面是使用的例子 DataApi

一切都推到了 DataApi 的设备,并提供他们两人之间共享。可以改变这个数据在两侧和另一侧将被立即通知关于这样的改变(当设备被连接到彼此)。您也可以在任意时刻读取该数据(例如,当用户选择你的watchface上的手表 - 配置数据将是对你早已等候在那里)。

Everything pushed to the DataApi is shared between devices and available of both of them. You can change this data on both sides and the other side will be notified about such change immediately (when devices are connected to each other). You can also read this data at any moment (for example when user will choose your watchface on the Watch - the configuration data will be already waiting for you there).

public class WatchfaceConfigActivity extends Activity {
    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle connectionHint) {
                    }
                    @Override
                    public void onConnectionSuspended(int cause) {
                    }
            })
            .addOnConnectionFailedListener(new OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                    }
                })
            .addApi(Wearable.API)
            .build();
        mGoogleApiClient.connect();
    }

和您想要同步的新fconfiguration与Android Wear设备每次必须通过可穿戴 DataApi 来把DataRequest:

and every time you want to sync new fconfiguration with the Android Wear device you have to put a DataRequest via Wearable DataApi:

    private void syncConfiguration() {
        if(mGoogleApiClient==null)
            return;

        final PutDataMapRequest putRequest = PutDataMapRequest.create("/CONFIG");
        final DataMap map = putRequest.getDataMap();
        map.putInt("mode", 1);
        map.putInt("color", Color.RED);
        map.putString("string_example", "MyWatchface");
        Wearable.DataApi.putDataItem(mGoogleApiClient,  putRequest.asPutDataRequest());
    }
}



您需要创建一个扩展的类 WearableListenerService

You need to create a class that extends WearableListenerService:

public class DataLayerListenerService extends WearableListenerService {

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        super.onDataChanged(dataEvents);

        final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
        for(DataEvent event : events) {
            final Uri uri = event.getDataItem().getUri();
            final String path = uri!=null ? uri.getPath() : null;
            if("/CONFIG".equals(path)) {
                final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
                // read your values from map:
                int mode = map.getInt("mode");
                int color = map.getInt("color");
                String stringExample = map.getString("string_example");
            }
        }
    }
}

在声明它的 AndroidManifest

<service android:name=".DataLayerListenerService" >
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>


注意,这是只使用的例子。也许(而不是注册的实例 WearableListenerService )会有更好的为您打造在 mGoogleApiClient 的实例您Watchface直接添加的DataListener 有:


Notice that this is only an example of usage. Maybe (instead of registering an instance of WearableListenerService) there will be better for you to create an instance of mGoogleApiClient inside your Watchface directly and add a DataListener there:

    Wearable.DataApi.addListener(mGoogleApiClient, new DataListener() {
        @Override
        public void onDataChanged(DataEventBuffer dataEvents) {
            // read config here and update the watchface
        }
    });

也许你并不需要共享数据 - 那么你可以使用通讯 MessageApi 并发送,只有当新配置被保存的信息或然后看着想读取手机当前的配置

Maybe you don't need shared data - then you can communicate using MessageApi and send messages only when new configuration is saved or then watch wants to read current configuration from phone.

这篇关于Android的磨损Watchface设置主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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