Android Access Android Wear data layer api--sync

Source: Internet
Author: User
Tags object serialization

Data items It is used to synchronize the phone and wear data interfaces, a date items usually contains the following sections:

Payload

A byte array. Whether you need to set the data type, we agree to the object serialization and deserialization, size cannot exceed 100k.

Path

A unique string. Must start with a forward slash (for example, "/path/to/data")

There is usually no need to directly implement DataItem, just need to:

1. Create a Putdatarequest object. Specifies a path string that differs from the item

2. Call SetData () to set payload

3, call Dataapi.putdataitem (), request the system to create a data item

4. When the data item is requested, the system returns a correct data item object that implements the interface

Using raw bytes with SetData (), it is recommended to use a data map. Ability to expose an easy-to-use bundle-like data


Synchronizing data by using data map

Assuming you can. Using the Datamap class, the ability to use data items like bundles, and cashing serialization and deserialization helps you finish, and you are able to manipulate it using key-value pairs.

Steps to use Data map

1 Creating the Putdatamaprequest object, setting the path to the data item

2 calls PutDataMapRequest.getDataMap()创建data map,能够设置数据

3 设置须要的数据使用put...()方法,如putString()

4 call   putdatamaprequest.asputdatarequest () Create   putdatarequest object

5 call   Dataapi.putdataitem ()   Request system Create data item

Assuming the phone is not connected to the wear, the cache waits until the connection is synchronized

For example, the following code sees

Putdatamaprequest DataMap = putdatamaprequest.create ("/count");d Atamap.getdatamap (). Putint (Count_key, count++); Putdatarequest request = Datamap.asputdatarequest (); pendingresult<dataapi.dataitemresult> Pendingresult = Wearable.dataapi        . Putdataitem (MGoogleApiClient, request);

Listen for data item time

Assuming that one data layer changes data item, you may need to notify any changes to the connection, and you will be able to implement these events through time, a listening data item, here is a sample.


@Overridepublic void ondatachanged (Dataeventbuffer dataevents) {for    (dataevent event:dataevents) {        if ( Event.gettype () = = dataevent.type_deleted) {            log.d (TAG, "DataItem DELETED:" + Event.getdataitem (). GetURI ());        } else if (event.gettype () = = dataevent.type_changed) {             log.d (TAG, "DataItem CHANGED:" + Event.getdataitem (). GetURI () );        }    }}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android Access Android Wear data layer api--sync

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.