The ability to create fully-featured apps for Android wearables is a great way to provide a hands-free experience. However, to preserve battery, Android will quickly put the device to sleep, resulting in the experience being lost.
Sometimes, we want to be able to display content on the screen, even when the wearable goes to sleep or enters the ambient mode:
[assembly: UsesPermission(Manifest.Permission.WakeLock)]
[assembly: UsesLibrary( "com.google.android.wearable", false)]
Activity
, we can inherit from WearableActivity
:public class MainActivity : WearableActivity
SetAmbientEnabled()
method:protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetAmbientEnabled();
}
OnEnterAmbient()
and OnExitAmbient()
methods to perform the desired actions when the ambient mode changes:public override void OnEnterAmbient(Bundle ambientDetails) { base.OnEnterAmbient(ambientDetails); } public override void OnExitAmbient() { base.OnExitAmbient(); }
IsAmbient
property:boo linAmbientMode = IsAmbient;
public override void OnUpdateAmbient() { base.OnUpdateAmbient(); }
Sometimes, we create an app that we would like to use to display data on the screen, even after the device goes to sleep. This is most useful for a watch face; but it is also used in other apps, such as a fitness app. It is unnecessary for the device to be running at full power if the screen is going to be updated only in every few seconds or even minutes.
These types of apps are useful when only we want to allow the user to always be able to see the information, but when we do not want to keep the device awake all the time. When the device is awake, battery gets consumed. So having an app that can run in low-power ambient mode preserves battery as well as functioning as an always-on display.
The first thing that needs to be done is to get the permission of keeping the display awake. However, only the wearable app, and not the handheld, requires the permission. The handheld app must have the same or more permissions than the wearable app, because the wearable app cannot exceed the permissions of the handheld app. If we forget to add the permission to the handheld, the wearable app will not be installed.
To be able to use the always-on activity on the wearable, we must specify that we are going to use the com.google.android.wearable
library. If we want our app to be installed on the wearables running Android versions prior to version 5.1, we need to specify that the library is not required by passing false
as the second parameter. If the device is not running the appropriate version of Android or does not support the ambient mode, then the activity will function as a normal activity.
For utilizing the activity that is aware of the ambient mode, our activity should inherit from WearableActivity
instead of Activity
. This type provides features that inform us about when the devices enter or leave the ambient mode. To be notified of these events, we need to request that the activity to runs in ambient mode. We do this by invoking the SetAmbientEnabled()
method in OnCreate
.
As soon as the device enters the ambient mode or goes to sleep, the activity's OnEnterAmbient()
method will be invoked. Conversely, when the device leaves the ambient mode or wakes up, the OnExitAmbient()
method is invoked. We can override these methods to handle the transitions, allowing our app to adjust. If we need to verify that we are in the ambient mode, we can easily do so using the value of the IsAmbient
property.
When in the ambient mode, the activity will invoke the OnUpdateAmbient()
method once in a minute so that we can update the screen with information for the user. Although, we can update the screen more frequently using alarms, we should not exceed an update once every ten seconds.
When updating the screen in the ambient mode, we should reduce the amount of processing and colors displayed to reduce the battery consumption. Usually, we would specify a black background and minimal white graphics or text. Since the ambient mode does not receive events, we can disable any kind of interactive element.