微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

主机上的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进行通信并仅在保存新配置时发送消息,或者然后监视想要从电话读取当前配置.

原文地址:https://www.jb51.cc/android/1071442.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐