主机上的Android Wear Watchface设置

我目前有一个开发的android磨损表盘.然而,我现在想在主机应用程序上创建一个允许用户自定义表盘的设置部分.我是android开发的新手,所以我很好奇这样做的正确方法.

有没有办法在主机上更新共享首选项,然后在磨损设备上推送或同步共享首选项?或者我应该看到一个完全不同的方式?

解决方法:

您可以使用DataApi或MessageApi在Phone和Watch设备之间同步您的watchface配置.

请查看文档并选择更符合您需求的文档:
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的所有内容都在设备之间共享,并且可以同时使用.您可以双方更改此数据,另一方将立即通知此类更改(当设备相互连接时).您也可以随时读取此数据(例如,当用户在Watch上选择您的表盘时 – 配置数据将在那里等待您).

在电话方面:

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

每次要将新配置与Android Wear设备同步时,您必须通过Wearable DataApi放置DataRequest:

    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的类:

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的实例)你最好直接在Watchface中创建一个mGoogleapiclient实例并在那里添加一个DataListener:

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

也许您不需要共享数据 – 然后您可以使用MessageApi进行通信并仅在保存新配置时发送消息,或者然后监视想要从电话读取当前配置.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...