From 8f8e87be3cbd30496b8ec66c6e8aee83d1cfdc56 Mon Sep 17 00:00:00 2001
From: Luan Nguyen Some Wear apps are most useful when they are constantly visible to the user. For example, users
+out on a run can glance at their wearable to see the distance covered and time elapsed, or after
+recording a grocery list on their wearable, users can quickly see which items are remaining on the
+list as they shop at the market. Making an app constantly visible has an impact on battery life,
+so you should carefully consider that impact when adding this feature to your app.
+ Android Wear devices running Android version 5.1 or higher allow apps to remain in the
+foreground while saving battery power. Android Wear apps can control what’s displayed on the
+wearable device screen while the device is in a low-power ambient mode. Wear apps that run in both
+ambient and interactive mode are called always-on apps.
+ This lesson describes how to enable your wearable app to be always-on, update the screen
+while in ambient mode, and maintain backwards compatibility.
+ For new and existing projects, you can add ambient mode support to your Wear app by updating
+your development project configuration. After you complete the project configuration, extend the
+ In order to support ambient mode in your Wear app, you must update your Android SDK and configure
+your development project. Follow these steps to make the necessary changes: Note: The To enable ambient mode in your activity, use the
+ Enable ambient mode in your activity as follows: If the user does not interact with your app for a period of time while it is displayed, or if
+the user covers the screen with their palm, the system switches the activity to ambient mode.
+After the app switches to ambient mode, update the activity UI to a more basic layout to reduce
+power consumption. You should use a black background with minimal white graphics and text. To
+ease a user into the transition from interactive to ambient mode, try to maintain similar placement
+of items on the screen. For more information on presenting content on an ambient screen, see the
+Watch Faces for Android Wear
+design guide.
+ Note: In ambient mode, disable any interactive elements on the
+screen, such as buttons. For more information on how to design user interactions for an always-on
+app, see the
+App Structure for Android Wear design
+guide.
+ When the activity switches to ambient mode, the system calls the
+ When the user taps the screen or brings up their wrist, the activity switches from ambient mode
+to interactive mode. The system calls the
+ The following code snippet shows how to change the text color to green and enable anti-aliasing
+when the system switches to interactive mode: Ambient mode allows you to update the screen with new information for the user, but you must
+carefully balance display updates against the battery life. You should strongly consider only
+overriding the
+ In order to preserve battery power, most wear apps should not frequently update the screen while
+in ambient mode. We recommend designing your app to update the screen once per minute while
+in this mode. The system provides a callback method,
+ To update your app content, override the
+ For apps that require more frequent updates, such as a fitness, time-keeping, and travel
+information apps, use an
+ To implement an alarm that updates content more frequently in ambient mode, follow these steps:
+ Note: The alarm manager may create new instances of your activity as they are
+triggered. To prevent this situation, ensure that your activity is declared with the
+ The following sections describe these steps in detail. The alarm manager launches a pending intent that updates the screen and schedules the next alarm.
+The following example shows how to declare the alarm manager and the pending intent in the
+ When the alarm triggers and launches the pending intent, update the screen and schedule the next
+alarm by overriding the
+ In this example activity, the alarm manager triggers every 20 seconds in ambient mode. When
+the timer ticks, the alarm triggers the intent to update the screen and then sets the delay for the
+next update.
+ The following example shows how to update information on the screen and set the alarm for the
+next update:
+ Schedule the alarm to update the screen when the activity is entering ambient mode or when the
+activity is already in ambient mode by overriding the
+ Note: In this example, the When the device switches to interactive mode, cancel the alarm in the
+ When the user exits or stops your activity, cancel the alarm in the
+ Activities that support ambient mode automatically fall back to normal activities on Wear devices
+that are on Android versions prior to 5.1 (API level 22). No special app code is required to support
+devices on these versions of Android. When the device switches to ambient mode, the device returns
+to the home screen and exits your activity. If your app should not be installed or updated on devices with Android versions prior to 5.1,
+update your manifest with the following:
@@ -21,10 +21,6 @@ differ greatly in design and usability and the amount of functionality provided.
These are the main differences between handheld and wearable apps:This lesson teaches you to
+
+
+You should also read
+
+Related Samples
+
+
+Enable Ambient Mode in a Wearable App
+
+WearableActivity
+class, which provides all the methods you need to enable ambient mode in your app. The following
+sections describe these steps in detail.Configure your development project
+
+
+
+
+targetSdkVersion to 22
+ or higher.minSdkVersion to
+ 20 or higher, if you want to support devices on versions prior to Android 5.1. For more
+ information on backwards compatibility, see
+ Maintain Backward-compatibility.build.gradle file:
+
+dependencies {
+ ...
+ compile 'com.google.android.support:wearable:1.2.0'
+ provided 'com.google.android.wearable:wearable:1.0.0'
+}
+
+provided dependency ensures that the classes loaded at
+run-time to support ambient mode are also available at compile-time.
+
+<application>
+ <uses-library android:name="com.google.android.wearable"
+ android:required="false" />
+ ...
+</application>
+
+ WAKE_LOCK
+ permission to the handheld and wearable app manifest:
+
+<uses-permission android:name="android.permission.WAKE_LOCK" />
+
+ Create an activity that supports ambient mode
+
+WearableActivity
+class and methods.
+
+
+
+WearableActivity.onCreate()
+ method of your activity, call the
+ setAmbientEnabled()
+ method.
+public class MainActivity extends WearableActivity {
+
+@Override
+public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setAmbientEnabled();
+ ...
+}
+
+
+Handle transitions between modes
+
+onEnterAmbient()
+method in your wearable activity. The following code snippet shows how to change the text color to
+white and disable anti-aliasing after the system switches to ambient mode:
+
+@Override
+public void onEnterAmbient(Bundle ambientDetails) {
+ super.onEnterAmbient(ambientDetails);
+
+ mStateTextView.setTextColor(Color.WHITE);
+ mStateTextView.getPaint().setAntiAlias(false);
+}
+
+
+onExitAmbient()
+method. Override this method to update the UI layout so that your app displays in a full-color,
+interactive state.
+@Override
+public void onExitAmbient() {
+ super.onExitAmbient();
+
+ mStateTextView.setTextColor(Color.GREEN);
+ mStateTextView.getPaint().setAntiAlias(true);
+}
+
+
+Update Content in Ambient Mode
+
+onUpdateAmbient()
+method to update the screen once a minute in ambient mode. If your app requires more frequent
+updates, take into consideration that there is a trade-off between battery life and the frequency of
+updates. To realize battery savings, updates should be no more than once every 10 seconds. In
+practice, however, you should update your app less frequently than that.
+Update once a minute
+
+onUpdateAmbient(),
+that allows you to update the screen at this recommended frequency.onUpdateAmbient()
+method in your wearable activity:
+
+@Override
+public void onUpdateAmbient() {
+ super.onUpdateAmbient();
+
+ // Update the content
+}
+
+
+Update more frequently
+
+AlarmManager
+object to wake the processor and update the screen more frequently.
+
+
+android:launchMode="singleInstance" parameter in the manifest.Prepare the alarm manager
+
+onCreate()
+method of your activity:
+private AlarmManager mAmbientStateAlarmManager;
+private PendingIntent mAmbientStatePendingIntent;
+
+@Override
+public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setAmbientEnabled();
+
+ mAmbientStateAlarmManager =
+ (AlarmManager) getSystemService(Context.ALARM_SERVICE);
+ Intent ambientStateIntent =
+ new Intent(getApplicationContext(), MainActivity.class);
+
+ mAmbientStatePendingIntent = PendingIntent.getActivity(
+ getApplicationContext(),
+ 0,
+ ambientStateIntent,
+ PendingIntent.FLAG_UPDATE_CURRENT);
+ ...
+}
+
+
+onNewIntent()
+method:
+
+@Override
+public void onNewIntent(Intent intent) {
+ super.onNewIntent(intent);
+
+ setIntent(intent);
+
+ // Described in the following section
+ refreshDisplayAndSetNextUpdate();
+}
+
+
+Update screen and schedule data updates
+
+
+// Milliseconds between waking processor/screen for updates
+private static final long AMBIENT_INTERVAL_MS = TimeUnit.SECONDS.toMillis(20);
+
+private void refreshDisplayAndSetNextUpdate() {
+
+ if (isAmbient()) {
+ // Implement data retrieval and update the screen for ambient mode
+ } else {
+ // Implement data retrieval and update the screen for interactive mode
+ }
+
+ long timeMs = System.currentTimeMillis();
+
+ // Schedule a new alarm
+ if (isAmbient()) {
+ // Calculate the next trigger time
+ long delayMs = AMBIENT_INTERVAL_MS - (timeMs % AMBIENT_INTERVAL_MS);
+ long triggerTimeMs = timeMs + delayMs;
+
+ mAmbientStateAlarmManager.setExact(
+ AlarmManager.RTC_WAKEUP,
+ triggerTimeMs,
+ mAmbientStatePendingIntent);
+
+ } else {
+ // Calculate the next trigger time for interactive mode
+ }
+}
+
+
+Schedule the next alarm
+
+onEnterAmbient()
+method and the
+onUpdateAmbient()
+method:
+@Override
+public void onEnterAmbient(Bundle ambientDetails) {
+ super.onEnterAmbient(ambientDetails);
+
+ refreshDisplayAndSetNextUpdate();
+}
+
+@Override
+public void onUpdateAmbient() {
+ super.onUpdateAmbient();
+
+ refreshDisplayAndSetNextUpdate();
+}
+
+
+refreshDisplayAndSetNextUpdate()
+method is called whenever the screen needs to be updated. For more examples of when to call this
+method, see the AlwaysOn sample.
+Cancel the alarm
+
+onExitAmbient()
+method:
+@Override
+public void onExitAmbient() {
+ super.onExitAmbient();
+
+ mAmbientStateAlarmManager.cancel(mAmbientStatePendingIntent);
+}
+
+
+onDestroy()
+method of your activity:
+@Override
+public void onDestroy() {
+ mAmbientStateAlarmManager.cancel(mAmbientStatePendingIntent);
+ super.onDestroy();
+}
+
+
+Maintain Backward-compatibility
+
+
+<uses-library android:name="com.google.android.wearable" android:required="true" />
+
diff --git a/docs/html/training/wearables/apps/index.jd b/docs/html/training/wearables/apps/index.jd
index 812f8936eefc..e2a71de639c9 100644
--- a/docs/html/training/wearables/apps/index.jd
+++ b/docs/html/training/wearables/apps/index.jd
@@ -6,12 +6,12 @@ page.image=wear/images/01_create.png
@jd:body
Dependencies and Prerequisites
-
-
-Dependencies and Prerequisites
+
+
+
-
To conserve power on a wearable device, you can enable ambient mode for your Wear app. +Devices transition from interactive to ambient mode when the user is idle on an activity or when +the user covers the screen with their palm. Wearable apps that can transition into ambient mode are +called always-on apps. The following describes the two modes of operation for always-on apps: +
+On devices running versions prior to Android 5.1 or for apps that do not support ambient mode, +when a user is idle on an activity or when the user covers the screen with their palm on an +activity, the Wear home screen is displayed instead of your activity in ambient mode. If you +need to show persistent content on versions prior to Android 5.1, create a notification in the +context stream instead.
+Note: We recommend using Android Studio for Android Wear development as it provides project setup, library inclusion, and packaging conveniences that aren't available in ADT. The rest of this training assumes you're using Android Studio. @@ -64,6 +80,9 @@ in ADT. The rest of this training assumes you're using Android Studio.