我可以发送自定义对象到Android Wear? [英] Can I send custom objects to Android Wear?

查看:240
本文介绍了我可以发送自定义对象到Android Wear?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学如何开发Android Wear,我创建了一个全屏幕的活动为智能手表和我得到一些JSON数据,并创建这个自定义对象列表的应用程序我的手机的一部分。

I am just learning how to develop for Android Wear, I have created a full screen Activity for Smart Watches and in my mobile part of the application I get some JSON data and create a list of custom objects from this.

在我的手机应用程序我显示的信息在ListView这些对象。

On my mobile app I show the information for these objects in a ListView.

上穿一件我的应用我想说明的有限版本的名单,例如前3名的名单将在展会上的可穿戴我的全屏应用程序。

On the Wear piece of my application I want to show a limited version of this list, for example the top 3 from the list will be show on my full screen app on the Wearable.

我的问题是,似乎没有一种方法来发送Parcelable对象Android Wear,有在的DataItem没有选择putParcelable。

My problem is that there doesn't seem to be a way to send Parcelable Objects to Android Wear, there is no option to putParcelable in the DataItem.

它看起来像唯一的选择就是以字节为单位发送的对象,像这样的:

It looks like the only option is to send an object in bytes, like this:

public void sendWearableData(GoogleApiClient aGoogleApiClient, ArrayList<MyObject> myObjectList, String path)
{

    googleApiClient = aGoogleApiClient;

    byte[] testBytes = new byte[0];

    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(path);
        try {
            testBytes = BytesUtils.toByteArray(myObjectList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataMapRequest.getDataMap().putByteArray(Constants.TEST_KEY, testBytes);
        PutDataRequest request = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, request);
    }
}

所以,我有我的对象转换为字节,它发送到Android Wear并将其转换回?这意味着,我的对象,我已经上实现Parcelable这样我就可以通过意图送他们现在还需要实现Serializable接口,这是正确的还是有这样做的更好的办法?

So I have to convert my object to bytes, send it to Android Wear and convert it back? This means that my Objects which I have implemented Parcelable on so I can send them via Intents now also need to implement Serializable, is this correct or is there a better way of doing it?

推荐答案

在我的应用程序,我创建了一个轻量级类,特别是从手机看发送。因为code是应用程序的移动和耐磨零件之间共享它可以很容易地包装和这两个设备没有code复制的恢复。它提供了捆绑样的机制,但使用数据映射

Bundle-like solution:

In my app I've created a lightweight class, specially for sending it from phone to watch. Because the code is shared between mobile and wearable parts of app it can be easily packed and restored on both devices without code duplication. It provides Bundle-like mechanism, but using DataMap.

实现:

public class MyObject {

    public final long itemId;
    public final long sortOrder;
    public final String priceString;

    public MyObject(long itemId, long sortOrder, String priceString) {
        this.itemId = itemId;
        this.sortOrder = sortOrder;
        this.priceString = priceString;
    }

    public MyObject(DataMap map) {
        this(map.getLong("itemId"),
             map.getLong("sortOrder"),
             map.getString("priceString")
            );
    }

    public DataMap putToDataMap(DataMap map) {
        map.putLong("itemId", itemId);
        map.putLong("sortOrder", sortOrder);
        map.putString("priceString", priceString);
        return map;
    }
}

写这样的课程将让你考虑究竟需要在发送设备之间发送尽可能少。它也不会打破时任何场将被添加或删除(在反对到下一溶液)。

Writing such class will let you consider the what actually needs to be send between devices to send as little as possible. It will also not break when any field will be added or removed (in oppose to the next solution).

如果你不想写新的类,并想重新使用现有的code,你可以尝试使用下面的code。它可以让你留在唯一的 Parcelable 界面(而不需要实施序列化接口)。我没有测试它,而跨设备的成功发送,但它马歇尔()解组()字节数组/从包裹,并将其存储在数据映射

If you don't want to write the new class and want to reuse your existing code you can try to use the code below. It will let you stay with only Parcelable interface (without need to implement Serializable interface). I haven't tested it while sending across devices but it successes to marshall() and unmarshall() byte array to/from Parcel and store it in DataMap.

注意:我不知道究竟如何谷歌播放服务容纳所有这些 DataApi 数据,但我担心的东西可能会破坏当这样的类将被更新。照片 例如,类将更新Android Wear,用户将启动应用程序,将试图读取 DataApi 当前数据(这是连载使用旧的版本,这类),并尝试从字节[] ,如果它是更新版本阅读。这些问题应进行测试,但我不认为他们做了 DataApi 那么原始只是因为或者使更难制定磨损的应用程序。

NOTE: I don't know exactly how Google Play Services hold all these DataApi data, but I'm afraid that something may break when such class will be updated.
For example the class will be updated on Android Wear, user will launch the app that would try to read the current data from DataApi (that was "serialized" using old version of this class) and try to read it from byte[] as if it was updated version. These concerns should be tested, but I don't think that they made DataApi so primitive "just because" or to make harder to develop apps on Wear.

我强烈建议您使用捆绑式的解决方案,并且不使用 Parcelable 解决方案。照片 使用这个风险自担。

I strongly recommend to use Bundle-like solution and to not use the Parcelable solution.
Use this at your own risk.

import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.gms.wearable.DataMap;

/**
 * <p>Allows to put and get {@link Parcelable} objects into {@link DataMap}</p>
 * <b>USAGE:</b>
 * <p>
 * <b>Store object in DataMap:</b><br/>
 * DataMapParcelableUtils.putParcelable(dataMap, "KEY", myParcelableObject);
 * </p><p>
 * <b>Restore object from DataMap:</b><br/>
 * myParcelableObject = DataMapParcelableUtils.getParcelable(dataMap, "KEY", MyParcelableObject.CREATOR);
 * </p>
 * I do <b>not recommend</b> to use this method - it may fail when the class that implements {@link Parcelable} would be updated. Use it at your own risk.
 * @author Maciej Ciemięga
 */
public class DataMapParcelableUtils {

    public static void putParcelable(DataMap dataMap, String key, Parcelable parcelable) {
        final Parcel parcel = Parcel.obtain();
        parcelable.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        dataMap.putByteArray(key, parcel.marshall());
        parcel.recycle();
    }

    public static <T> T getParcelable(DataMap dataMap, String key, Parcelable.Creator<T> creator) {
        final byte[] byteArray = dataMap.getByteArray(key);
        final Parcel parcel = Parcel.obtain();
        parcel.unmarshall(byteArray, 0, byteArray.length);
        parcel.setDataPosition(0);
        final T object = creator.createFromParcel(parcel);
        parcel.recycle();
        return object;
    }
}

在code,也可在 GitHub上

这篇关于我可以发送自定义对象到Android Wear?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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