1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 Copyright 2013 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 --> 17 18 19 20 <sample> 21 <name>SynchronizedNotifications</name> 22 <group>Wearable</group> 23 <package>com.example.android.wearable.synchronizednotifications</package> 24 25 26 27 <!-- change minSdk if needed--> 28 <minSdk>18</minSdk> 29 30 31 <strings> 32 <intro> 33 <![CDATA[ 34 This sample creates simple or synchronized notifications on a 35 device and an Android Wear watch. 36 ]]> 37 </intro> 38 </strings> 39 40 <template src="base"/> 41 <template src="SimpleView"/> 42 <template src="WearPlusShared"/> 43 <common src="logger"/> 44 <common src="activities"/> 45 46 <metadata> 47 <status>PUBLISHED</status> 48 <categories>Wearable</categories> 49 <technologies>Android</technologies> 50 <languages>Java</languages> 51 <solutions>Mobile</solutions> 52 <level>INTERMEDIATE</level> 53 <icon>screenshots/web-icon.png</icon> 54 <screenshots> 55 <img>screenshots/different_notifications_phone.png</img> 56 <img>screenshots/different_notifications_wearable.png</img> 57 <img>screenshots/notification_options.png</img> 58 <img>screenshots/watch_only_notification.png</img> 59 </screenshots> 60 <api_refs> 61 <android>com.google.android.gms.wearable.DataApi</android> 62 <android>com.google.android.gms.wearable.Wearable</android> 63 <android>com.google.android.gms.wearable.WearableListenerService</android> 64 </api_refs> 65 66 <description> 67 <![CDATA[ 68 A basic sample showing how to use simple or synchronized notifications. 69 This allows users to dismiss events from either their phone or wearable device simultaneously. 70 ]]> 71 </description> 72 73 <intro> 74 <![CDATA[ 75 The [DataAPI][1] exposes an API for components to read or write data items and assets between 76 the handhelds and wearables. A [DataItem][2] is synchronized across all devices in an Android Wear network. 77 It is possible to set data items while not connected to any nodes. Those data items will be synchronized 78 when the nodes eventually come online. 79 80 This example presents three buttons that would trigger three different combinations of 81 notifications on the handset and the watch: 82 83 1. The first button builds a simple local-only notification on the handset. 84 2. The second one creates a wearable-only notification by putting a data item in the shared data 85 store and having a [com.google.android.gms.wearable.WearableListenerService][3] listen for 86 that on the wearable. 87 3. The third one creates a local notification and a wearable notification by combining the above 88 two. It, however, demonstrates how one can set things up so that the dismissal of one 89 notification results in the dismissal of the other one. 90 91 In the #2 and #3 items, the following code is used to synchronize the data between the handheld 92 and the wearable devices using DataAPI. 93 94 ```java 95 PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(path); 96 putDataMapRequest.getDataMap().putString(Constants.KEY_CONTENT, content); 97 putDataMapRequest.getDataMap().putString(Constants.KEY_TITLE, title); 98 PutDataRequest request = putDataMapRequest.asPutDataRequest(); 99 Wearable.DataApi.putDataItem(mGoogleApiClient, request) 100 .setResultCallback(new ResultCallback<DataApi.DataItemResult>() { 101 @Override 102 public void onResult(DataApi.DataItemResult dataItemResult) { 103 if (!dataItemResult.getStatus().isSuccess()) { 104 Log.e(TAG, "buildWatchOnlyNotification(): Failed to set the data, " 105 + "status: " + dataItemResult.getStatus().getStatusCode()); 106 } 107 } 108 }); 109 ``` 110 111 [1]: http://developer.android.com/reference/com/google/android/gms/wearable/DataApi.html#putDataItem(com.google.android.gms.common.api.GoogleApiClient%2C%20com.google.android.gms.wearable.PutDataRequest) 112 [2]: http://developer.android.com/reference/com/google/android/gms/wearable/DataItem.html 113 [3]: https://developer.android.com/reference/com/google/android/gms/wearable/WearableListenerService.html 114 ]]> 115 </intro> 116 </metadata> 117 </sample> 118