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