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