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>ElizaChat</name> 22 <group>Wearable</group> 23 <package>com.example.android.wearable.elizachat</package> 24 25 26 27 <!-- change minSdk if needed--> 28 <minSdk>18</minSdk> 29 30 31 <strings> 32 <intro> 33 <![CDATA[ 34 This sample is a phone application that provides a chat experience in which users can respond to 35 messages with a quick voice response. New messages create a notification with a "Reply" action. 36 The notification is bridged from phone to wearable, and selecting the "Reply" action on the 37 wearable opens the voice transcription UI allowing the user to speak a response. 38 ]]> 39 </intro> 40 </strings> 41 42 <template src="base"/> 43 44 <metadata> 45 <status>PUBLISHED</status> 46 <categories>Wearable</categories> 47 <technologies>Android</technologies> 48 <languages>Java</languages> 49 <solutions>Mobile</solutions> 50 <level>INTERMEDIATE</level> 51 <icon>screenshots/wearable_eliza_notification.png</icon> 52 <screenshots> 53 <img>screenshots/companion_eliza_chat_response.png</img> 54 <img>screenshots/companion_eliza_chat.png</img> 55 <img>screenshots/wearable_eliza_notification.png</img> 56 <img>screenshots/wearable_voice_reply.png</img> 57 </screenshots> 58 <api_refs> 59 <android>android.support.v4.app.NotificationCompat.WearableExtender</android> 60 </api_refs> 61 62 <description> 63 <![CDATA[ 64 A basic sample showing how to add extensions to notifications on wearable using 65 [NotificationCompat.WearableExtender][1] API by providing a chat experience. 66 ]]> 67 </description> 68 69 <intro> 70 <![CDATA[ 71 [NotificationCompat.WearableExtender][1] API offers you a functionality to 72 add wearable extensions to notifications like [Add the Voice Input as a Notification Action][2]. 73 74 This example also adds the wearable notifications as a voice input by the following code: 75 76 ```java 77 NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 78 .setContentTitle(getString(R.string.eliza)) 79 .setContentText(mLastResponse) 80 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza)) 81 .setSmallIcon(R.drawable.bg_eliza) 82 .setPriority(NotificationCompat.PRIORITY_MIN); 83 84 Intent intent = new Intent(ACTION_RESPONSE); 85 PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 86 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT); 87 Notification notification = builder 88 .extend(new NotificationCompat.WearableExtender() 89 .addAction(new NotificationCompat.Action.Builder( 90 R.drawable.ic_full_reply, getString(R.string.reply), pendingIntent) 91 .addRemoteInput(new RemoteInput.Builder(EXTRA_REPLY) 92 .setLabel(getString(R.string.reply)) 93 .build()) 94 .build())) 95 .build(); 96 NotificationManagerCompat.from(this).notify(0, notification); 97 ``` 98 99 When you issue this notification, users can swipe to the left to see the "Reply" action button. 100 101 When you receive the response, you can do it by the following code: 102 103 ```java 104 @Override 105 public int onStartCommand(Intent intent, int flags, int startId) { 106 if (null == intent || null == intent.getAction()) { 107 return Service.START_STICKY; 108 } 109 String action = intent.getAction(); 110 if (action.equals(ACTION_RESPONSE)) { 111 Bundle remoteInputResults = RemoteInput.getResultsFromIntent(intent); 112 CharSequence replyMessage = ""; 113 if (remoteInputResults != null) { 114 replyMessage = remoteInputResults.getCharSequence(EXTRA_REPLY); 115 } 116 processIncoming(replyMessage.toString()); 117 } else if (action.equals(MainActivity.ACTION_GET_CONVERSATION)) { 118 broadcastMessage(mCompleteConversation.toString()); 119 } 120 return Service.START_STICKY; 121 } 122 ``` 123 124 [1]: https://developer.android.com/reference/android/support/v4/app/NotificationCompat.WearableExtender.html 125 [2]: https://developer.android.com/training/wearables/notifications/voice-input.html#AddAction 126 ]]> 127 </intro> 128 </metadata> 129 </sample> 130