From c2be8a6da0c193a0592586cfc8bdfdb7903cc055 Mon Sep 17 00:00:00 2001
From: Ricardo Cervera
-However, instead of working with raw bytes using setData(),
+However, instead of working with raw bytes using The following example shows how to create a data map and put data on it: The For example, here's what a typical callback looks like to carry out certain actions
-when data changes:
-This is just a snippet that requires more implementation details. Learn about
-how to implement a full listener service or activity in
+
+ For more information about handling the
+
+ If one side of the data layer connection changes a data item, you probably want
+to be notified of any changes on the other side of the connection.
+You can do this by implementing a listener for data item events. The code snippet in the following example notifies your app when the value of the
+counter defined in the previous example changes: This activity implements the
+
+ You can also implement the listener as a service. For more information, see
Listen for Data Layer
-Events.
-setData(),
we recommend you use a data map, which exposes
a data item in an easy-to-use {@link android.os.Bundle}-like interface.
increaseCounter() method in the following example shows how to create a
+data map and put data in it:
-PutDataMapRequest dataMap = PutDataMapRequest.create("/count");
-dataMap.getDataMap().putInt(COUNT_KEY, count++);
-PutDataRequest request = dataMap.asPutDataRequest();
-PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
- .putDataItem(mGoogleApiClient, request);
-
+public class MainActivity extends Activity implements
+ DataApi.DataListener,
+ GoogleApiClient.ConnectionCallbacks,
+ GoogleApiClient.OnConnectionFailedListener {
-Listen for Data Item Events
-If one side of the data layer connection changes a data item, you probably want
-to be notified of any changes on the other side of the connection.
-You can do this by implementing a listener for data item events.
+ private static final String COUNT_KEY = "com.example.key.count";
-
-@Override
-public 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());
- }
+ ...
+
+ // Create a data map and put data in it
+ private void increaseCounter() {
+ PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
+ putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
+ PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
+ PendingResult<DataApi.DataItemResult> pendingResult =
+ Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
}
+
+ ...
}
-PendingResult object, see
+Wait for the Status of Data
+Layer Calls.Listen for Data Item Events
+
+
+public class MainActivity extends Activity implements
+ DataApi.DataListener,
+ GoogleApiClient.ConnectionCallbacks,
+ GoogleApiClient.OnConnectionFailedListener {
+
+ private static final String COUNT_KEY = "com.example.key.count";
+
+ private GoogleApiClient mGoogleApiClient;
+ private int count = 0;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+
+ mGoogleApiClient = new GoogleApiClient.Builder(this)
+ .addApi(Wearable.API)
+ .addConnectionCallbacks(this)
+ .addOnConnectionFailedListener(this)
+ .build();
+ }
+
+ @Override
+ protected void onResume() {
+ super.onStart();
+ mGoogleApiClient.connect();
+ }
+
+ @Override
+ public void onConnected(Bundle bundle) {
+ Wearable.DataApi.addListener(mGoogleApiClient, this);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ Wearable.DataApi.removeListener(mGoogleApiClient, this);
+ mGoogleApiClient.disconnect();
+ }
+
+ @Override
+ public void onDataChanged(DataEventBuffer dataEvents) {
+ for (DataEvent event : dataEvents) {
+ if (event.getType() == DataEvent.TYPE_CHANGED) {
+ // DataItem changed
+ DataItem item = event.getDataItem();
+ if (item.getUri().getPath().compareTo("/count") == 0) {
+ DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
+ updateCount(dataMap.getInt(COUNT_KEY));
+ }
+ } else if (event.getType() == DataEvent.TYPE_DELETED) {
+ // DataItem deleted
+ }
+ }
+ }
+
+ // Our method to update the count
+ private void updateCount(int c) { ... }
+
+ ...
+}
+
+
+DataItem.DataListener interface. This activity adds itself as a listener
+for data item events inside the onConnected() method and removes the listener
+in the onPause() method.