Android Access Android Wear data layer api--sync

Source: Internet
Author: User

Data items is an interface for synchronizing phones and wear, and a date items typically includes the following sections:

Payload

A byte array, you can set any required data type, allow the object to be serialized and deserialized, the size can not exceed 100k.

Path

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

You do not usually need to implement DataItem directly, only:

1. Create Putdatarequest object, specify a path string to distinguish 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 (), we recommend using a data map that exposes an easy-to-use bundle-like data


Synchronizing data by using data map

If possible, using the Datamap class, you can use the data items like bundles, and cashing the serialization and deserialization helps you to do it, and you can 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

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

as shown in the following code

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

If one of the data layers changes the data item, you may need to notify any changes to the connection, and you can implement these through the time a listener data item event, below is an example.


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


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.