- Overview
- Transcript
2.6 WearableActivity and Ambient Mode
Added before Google I/O in 2015, ambient-mode activities prevent an application from kicking a user back out to the notification home screen when it would be more appropriate to keep the application running. This lesson will cover how to use ambient mode for your WearableListView
activity.
Related Links
1.Introduction and Getting Set Up3 lessons, 11:45
1.1Introduction02:46
1.2Setting Up Emulators and Hello World04:40
1.3Debugging on a Hardware Device04:19
2.Wearable App UI Components6 lessons, 1:15:54
2.1Round and Square Device Support06:37
2.2WearableListViews16:58
2.3DelayedConfirmationView10:06
2.4GridViewPager: Part 116:32
2.5GridViewPager: Part 216:10
2.6WearableActivity and Ambient Mode09:31
3.Wearable Notifications4 lessons, 38:35
3.1Using Basic and Multipage Notifications09:33
3.2Stacking Multiple Notifications and Using an Action Button10:04
3.3Replying With Speech, Emojis and Predefined Options08:57
3.4Creating Custom UI Notifications10:01
4.Creating a Watch Face2 lessons, 34:28
4.1Starting a Digital Watch Face16:57
4.2Handling Watch Face States17:31
5.Conclusion1 lesson, 02:14
5.1Conclusion02:14
2.6 WearableActivity and Ambient Mode
Hey everyone. This is Paul with Plus and you're watching the developing for Android Wear course. In this lesson we will go over a component that was recently introduced called the wearable activity. Wearable activity actually adds in support for ambient mode. So when your user clicks on their power button, if they have ambient mode enabled, they will be able to still see your activity while the screen is dimmed, rather than being kicked right back to the watch face screen. This will make it so that your users do not have to reopen your activity to use it as soon as they come back out of the power saving mode. But rather will always have your content on their screen that they can glance at as they need to. So to begin, let's go to Main Activity, and we will change it so that it no longer extends Activity, but rather it will extend Wearable Activity. And within Wearable Activity we have a couple of different methods that we can extend. So if you go down to just before Wearable List Adaptor, we will override on enter ambient. And we will also override on exit ambient. So what we will do in these methods, is when we enter ambient mode, we will save that in a variable that we can access through our adapter, so that we can change the items within the main activity list. And we will also set all of our backgrounds to be black when we enter ambient mode, or back to white when we exit it. So let's start by scrolling up to the top, and let's add a couple more variables here. So just before our Wearable List View, let's add public static boolean is Ambient Mode. And we'll set that to false as the default, and we will also wanna keep track of our box inset layout. So let's create a private box inset layout. M box inset layout. And next we will want to save a reference to box inset layout. We can go right under endless view, and on create, and we'll use M box inset layout is equal to A box Inset Layout, find View By Id. R.id.parent. And then if we go back to on Enter Ambient, we can set is Ambient Mode to true. And then mListView.setBackgroundColor to getResources.getColor. Android.R.color.black. And then we will do the exact same thing for m box inset layout. So mboxinsetlayout.setbackgroundcolorgetreso- urces.getcolorandroid.R.color.black. Let's make sure that the R is capitalized. And then finally we will use Mlistview.getAdapter.notifyDatasetChanged. So what notifyDatasetChanged will do is let our adapter know that the content should be redrawn on the screen. So when we modify our adapter to handle ambient mode later in this lesson it will know to redraw those items using the proper color scheme. If we go to on exit ambient, it will do the exact opposite here. So is ambient mode is equal to false. mListview.setBackgroundColor getResources.getColor Android.R.color.white. And then we will do the same for mBoxInsetLayout. So let's go ahead and paste that in and change it to mBoxInsetLayout. And then we will use mListView.getAdaptor. .notifyDataSetChanged. Now we'll need to update our wearable list adapter so that the content within our wearable list is showing the proper color for our text. So we will scroll down to the wearable list adapter and in onBindViewHolder we will add a couple lines just under setText that will have if MainActivity.IsAmbientMode, ViewHolder.TextView.setTextColor viewHolder.TextView.getResources, and we're using getResources from the TextView because the TextView itself has a context, so it just allows us to easily get to our resources. Then we will use .getColor, Android.R.color.white, because the text will be white on a black background. And if we were leaving ambient mode, we'll say else Viewholder.textView.setTextColor viewholder.textView.getResources.getColor android.R.color.black. The other method we will want to override, is on view recycled. So we'll go right below on bind view holder, and we'll override on view recycled. And we will say, view holder is equal to a casted over version of view holder. And if mainactivity.isambientmode, viewholder.textview.settextcolor to viewholder.textview.getresources.getcolor Android.R.color.white. Once that's done, all we need to do is go into our manifest under wear, source, main, and android manifest.xml. And we'll need to add the wake lock permission. As well as let the application know that we are using the Wearable library but it is not required for devices that are running API 21 or below. So first, let's go under Uses Feature and we will say Uses Permission android.permission.wakelock. And within our application node, we'll add a new line, just before our activity declarations. And we will say uses-library com.google.android.wearable, and android required is false. Once that's done, there is one more thing that we will need to add to main activity, and on create, we need to tell it that it will be using ambient mode, so just after set content view, we will add set ambient enabled, and then we can go ahead and hit run, we'll hit okay, and then we will go back to our emulator and if we hit on the power button you'll notice that our content now is black or white. It looks like we are setting the main item to black in the center as it scrolls. And that would be because if we go back to main Java views and wearable list item layout, we will simply need to go down to on center position, and on noncenter position and set it so that they only change the color to black or darker gray if ambient mode is not on. So we will say and MainActivity.isAmbientMode with the exclamation point right before it to say that it is not in ambient mode. And we'll put the same thing in on non-centered position. We will go ahead and hit run again. We'll hit okay. We'll go back to our emulator. We'll go ahead and hit the power button to put us into ambient mode. And you'll notice that everything is now black and white. Now there is a weird glitch with the emulator that allows you to scroll while you're in ambient mode. On a normal hardware watch, touching the screen will take you out of ambient mode, so you shouldn't have to deal with a lot of these edge cases, but it is what it is when you're using the emulator. So it's just something to be aware of. And that will finish our section on using native UI components within Android Wear. In the next section, we will actually go over the set of notifications that you can view on the main screen of your wearable device and how you can interact with your user using those. If you do want to learn more about the native UI components, you can go to your web browser and look up the creating custom layouts documentation on the developers.android.com site and there's a nice little list here of different components that are available specifically for Android Wear. So so far in this course we have already talked about the box inset layout, the card fragment. We've touched on the circle image view when we went over the delayed confirmation view. We've also talked about the grid view pager, and fragment grid pager adapter. The dots page indicator, the WatchViewstub, and the Wearable listview. We will talk about the confirmation activity later in this course. And of course you can use any other view that was created for Android, that is not specifically made for the wearable devices, but they will still work. So again, in the next set of lessons, we will start looking at the notification types that are allowed for Android Wear, and how to implement them in your application. So I will see you then.