Android Wear数据项 [英] Android Wear Data Items

查看:143
本文介绍了Android Wear数据项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何从手机的一些数据同步到Android Wear设备,并且我对数据项,但是我现在还是不清楚究竟如何在developer.android.com文章使用它们。具体来说,每个code段(GoogleApiClient,同步,和听),应加以落实,其中,在应用程序code中的流动和哪个设备,手机或磨损,或两者上。

I'm trying to figure out how to sync some data from the phone to the Android Wear device and I've read the article on developer.android.com on Data Items however I'm still not clear on exactly how to use them. Specifically where each code segment (the GoogleApiClient, the Sync, and the Listen) should be implemented both where in the flow of the app code and on which device, phone or wear, or both.

链接以developer.android.com页

Link to developer.android.com page

推荐答案

你看的样品API 20?有 DataApi 使用的 DataLayer 样品设在这里一个很好的示范:

Have you looked at the samples for API 20? There is a nice demonstration of DataApi usage in DataLayer sample located here:

{android-sdk-root}\samples\android-20\wearable\DataLayer

此外,我已经发布了使用 DataApi 在我的的 Android的磨损Watchface设置在主机
但可能是由于这个问题的称号,有没有简单的关系,与 DataApi 。所以也许这里是个好地方,再张贴 - 希望更多的用户会发现这个例子。请参见下面的code:

Also I've posted an example for usage of DataApi in my answer for Android Wear Watchface Settings on host
But probably due to the title of that question, there is no simply relation with DataApi. So maybe here is the good place to post it again - hopefully more users will find this example. Please see the code below:

一切都推到了 DataApi 的设备,并提供他们两人之间共享。可以改变这个数据在两侧和另一侧将被立即通知关于这样的改变(当设备被连接到彼此)。您还可以阅读在任何时刻保存的数据。下面是几个简单的步骤样本code实施 DataApi

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 saved data at any moment. Here is a sample code for implementing DataApi in few simple steps.

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 syncSampleDataItem() {
        if(mGoogleApiClient==null)
            return;

        final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE");
        final DataMap map = putRequest.getDataMap();
        map.putInt("color", Color.RED);
        map.putString("string_example", "Sample String");
        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("/SAMPLE".equals(path)) {
                final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
                // read your values from map:
                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>

这篇关于Android Wear数据项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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