Creating always-on wearable apps

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.

How to do it...

Sometimes, we want to be able to display content on the screen, even when the wearable goes to sleep or enters the ambient mode:

  1. The first thing that we need to do is add the wake lock permission requirement. We need to add this to both, wearable and handheld, as the wearable cannot have any permission that the handheld does not have:
    [assembly: UsesPermission(Manifest.Permission.WakeLock)]
  2. Next, in the wearable app, we need to specify that we are going to be using the wearable library if it is available:
    [assembly: UsesLibrary(
      "com.google.android.wearable", false)]
  3. Now, we can begin implementing the always-on activity. Instead of inheriting from Activity, we can inherit from WearableActivity:
    public class MainActivity : WearableActivity
  4. Then, we can request to be notified when the ambient mode changes, by invoking the SetAmbientEnabled() method:
    protected override void OnCreate(Bundle bundle) {
      base.OnCreate(bundle);
      SetAmbientEnabled();
    }
  5. Finally, we can override the 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();
    }
  6. We can detect whether the device is in ambient mode using the IsAmbient property:
    boo linAmbientMode = IsAmbient;
  7. There is also a special method that will trigger once every minute so that we can update the UI when in ambient mode:
    public override void OnUpdateAmbient() {
      base.OnUpdateAmbient();
    }

How it works...

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.

Note

The ambient mode is a low-power mode that allows an app to run when the device goes to sleep, but with more limited capacity.

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.

Tip

A wearable app can only have a subset of the permissions of the handheld app.

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.

Tip

When in the ambient mode, the device should not be woken up more than once in every 10 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.77.71