3.3 Replying With Speech, Emojis and Predefined Options
Since people can only use one hand to interact with wearable devices, speech is an important avenue for input. This lesson will show you how to add voice replies to notifications, as well as how to provide predefined options and emoji responses.
1.Introduction and Getting Set Up3 lessons, 11:45
2.Wearable App UI Components6 lessons, 1:15:54
3.Wearable Notifications4 lessons, 38:35
4.Creating a Watch Face2 lessons, 34:28
5.Conclusion1 lesson, 02:14
3.3 Replying With Speech, Emojis and Predefined Options
Hey everyone. This is Paul with Tuts Plus, and you're watching the Developing for Android Wear course. In this lesson, we will introduce another notification type which will allow your user to speak into their Android Wear Device in order to send information to your application. So, in order to start, let's go ahead and create a new method. So we'll call this private void, show reply notification. And let's go ahead and create our Notification Builder. So Notification Compact.Builder. Builder is equal to and we will use the same method that we've used in previous lessons. So get base Notification Builder. And on of the main components to a voice reply notification is the Remote Input Object. So let's go ahead and create a RemoteInput. And we'll call it RemoteInput = new RemoteInput.builder. And we will want to use the V4.app version of this. So let's go ahead and import that. And let's pass in a key of reply. And since this is a builder we can use other methods with this. So we'll use dot set label and we'll just call this label. And then we can us .build. So using this Remote Input, we will need to associate this with a pending intent. So let's go ahead and create an intent. Let's call this ReplyIntent is equal to new Intent. We'll pass in our content and then we'll use MainActivity.class as the destination for this intent. So when our user speaks into their watch, when prompted, they will be taken to MainActivity and whatever they say will be taken into that activity. Now let's go ahead and create a Pending Intent that will work with this intent. So we will use Pending Intent reply Pending Intent = Pending Intent.get Activity. We'll pass in our context, 0 our reply intent, and then Pending Intent.Flag Update Current. And then we will use this Pending Intent to add it to our notification builder. So let's say Builder.extend new wearable extender and then .Addaction and we will use a new notification Compat.action and then a .Builder. And our builder actually accepts a couple different parameters. So lets use an opening parenthesis, and it will need an ID for a drawable. So we'll use R. map. map. IC launcher A title for our action, so we'll just use the word reply. And then we need to pass in our reply Pending Intent. So that it knows what to do when we click on the action button. And it looks like I actually left a pair of parenthesis right after notification compat, so let's go ahead and get rid of those, because they were auto filled in here. And then after the action builder, we can go ahead and use, .Add Remote Input. And let's add in our Remote Input and then let's go ahead and call build. Let's put in our semicolon and this should be everything we need to create a base voice reply notification. So we can go ahead and use notification manager compact. from, we'll pass in our context, .notify, we can go ahead and pass in an I.D., so we'll just use one, and then we'll use builder .Build to create the notification and broadcast that out to our wearable device. So what this will end up doing is displaying a notification that when the user clicks on the action button, it'll prompt them to either draw an emoji on api 22 or higher, or speak a reply so that that information can be sent to the android application. Now, if we want to give them a set of predefined answers, what we can do is we can go to our string-arrays.xml file, And we can just create a new string array. So we'll say string-array we'll give it a name of reply_ items. And then we can add a few items in here so we'll say item and then we'll just say item one and let's copy this and paste it in a couple of times. And then we'll change these numbers over to be 4, 3, 2 and then we'll leave 1 there. And we'll go back to main activity. And let's create a string array based off of our string array XML item. So we'll just say String, we'll put in our brackets, replies = getResources().getStringArray( R.array.reply_choices ). And then in our remote input builder, we'll add a new item just under set label called setChoices, and then we can pass our replies into that. Now, when the user sends off the reply, we will need to catch it within our activity that we are starting up with our Reply intent. So let's go back up to on create, which is the first method that will see this, so we will use if getIntent is not equal to null, which generally it shouldn't be, but we just want to be on the safe side. We will create a bundle called RemoteInput. And we will use the method RempteInput.getResultsFromIntent(), and we'll pass it in getIntent. And then we need to make sure that RemoteInput is not null. So we'll say that if RemoteInput= Null. And let me make sure my capitalization is correct. Then we will need to handle this information however we would in our application. So to make this easy, let's just go ahead and show whatever the reply is as a toast message. So we'll use Toast.maketex,t we'll pass in our context. And then RemoteInput.getCharSequence our key is reply, which is what we associated with our RemoteInput earlier. And then let's use .ToString so that it will be a string for our toast message. And then Toast.LENGTH_SHORT and then we'll finally use our .show method and this will create our toast message based off of the reply. So now that that's all set, we have one last thing that we need to do. So let's scroll down to onClick and let's just make sure that our wearable list view will perform an action when the item is clicked on. We'll use else if getString R.string.notification_reply.equalsignore- case on the title. We will call showReplyNotification, and then we will call finish, and that should be all set. So let's go ahead and run this. And it looks like I actually named this wrong. It should be r.array.reply_items. So we'll save and fix that. We'll go ahead and hit run again. And if you haven't started up your emulator, you can go ahead and do that. Otherwise, you can just use the emulator that is already running. So I'm gonna go ahead and start that. And then I will come back as soon as emulator is set up and the program is installed. Okay, and now that our application is installed, let's go ahead and scroll down. And we'll go to our voice reply notification item so we'll click on that and as you can see it looks like a normal notification but if we scroll over to the reply button, we can click on that and we're presented with a special voice reply screen which has our label at the top which is our prompt to the users so they know how to respond. An option to speak now which does not work on the emulator however, if you install on a real device, this will work and you will get whatever content you speak in your mic on the application. We have our list of quick replies and we also have a draw emoji item which was added in API 22. So if we click on that you can actually draw something and it'll take a best guess at what we drew. And we'll send that using the check box. You'll notice that we have a delayed confirmation view. And when it says done, it sent that lollipop icon to our main activity, and displayed it in a totes message. So if we go back to our notification, and we hit reply again, we can use one of our quick reply items and it should say item two on main activity. And that's it. So, in this lesson you have learned how to create voice reply notifications. And in the next lesson, we will look at how to create new notifications that don't have to use the card layout format. So, I will see you then.