1page.title=Building a Watch Face Service 2 3@jd:body 4 5<div id="tb-wrapper"> 6<div id="tb"> 7<h2>This lesson teaches you to</h2> 8<ol> 9 <li><a href="#CreateProject">Create and Configure Your Project</a></li> 10 <li><a href="#CallbackMethods">Implement the Service Callback Methods</a></li> 11 <li><a href="#RegisterService">Register the Service Implementation</a></li> 12</ol> 13<h2>You should also read</h2> 14<ul> 15 <li><a href="{@docRoot}design/wear/watchfaces.html">Watch Faces for Android Wear</a></li> 16</ul> 17</div> 18</div> 19 20<p>Watch faces in Android Wear are implemented as <a 21href="{@docRoot}guide/components/services.html">services</a> and packaged inside a <a 22href="{@docRoot}training/wearables/apps/index.html">wearable app</a>. When users install a 23handheld app that contains a wearable app with watch faces, these watch faces become available 24in the <a 25href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android 26Wear companion app</a> on the handheld device and in the watch face picker on the wearable. When 27users select one of the available watch faces, the wearable device shows the watch face and 28invokes its service callback methods as required.</p> 29 30<p>This lesson shows you how to configure an Android project to include watch faces and how 31to implement the watch face service.</p> 32 33 34 35<h2 id="CreateProject">Create and Configure Your Project</h2> 36 37<p>To create an Android project for your watch face in Android Studio:</p> 38 39<ol> 40<li>Start Android Studio.</li> 41<li>Create a new project: 42 <ul> 43 <li>If you don't have a project opened, in the <strong>Welcome</strong> screen, click 44 <strong>New Project</strong>.</li> 45 <li>If you have a project opened, from the <strong>File</strong> menu, select <strong>New 46 Project</strong>.</li> 47 </ul> 48</li> 49<li>Provide an application name and click <strong>Next</strong>.</li> 50<li>Select the <strong>Phone and Tablet</strong> form factor.</li> 51<li>Under <strong>Minimum SDK</strong>, choose API 18.</li> 52<li>Select the <strong>Wear</strong> form factor.</li> 53<li>Under <strong>Minimum SDK</strong>, choose API 21 and click <strong>Next</strong>.</li> 54<li>Select <strong>Add No Activity</strong> and click <strong>Next</strong> in the two following 55 screens.</li> 56<li>Click <strong>Finish</strong>.</li> 57<li>Click <strong>View</strong> > <strong>Tool Windows</strong> > <strong>Project</strong> in the 58 IDE window.</li> 59</ol> 60 61<p>Android Studio creates a project with the <code>wear</code> and <code>mobile</code> modules. 62For more information, see <a href="{@docRoot}sdk/installing/create-project.html">Creating a 63Project</a>.</p> 64 65<h3 id="Dependencies">Dependencies</h3> 66 67<p>The Wearable Support Library provides the necessary classes that you extend to create watch 68face implementations. The Google Play services client libraries (<code>play-services</code> and 69<code>play-services-wearable</code>) are required to sync data items between the companion device 70and the wearable with the <a href="{@docRoot}training/wearables/data-layer/index.html">Wearable 71Data Layer API</a>.</p> 72 73<p>Android Studio automatically adds the required entries in your <code>build.gradle</code> 74files when you create the project in the instructions above.</p> 75 76<h3 id="Reference">Wearable Support Library API Reference</h3> 77 78<p>The reference documentation provides detailed information about the classes you use to 79implement watch faces. Browse the 80<a href="{@docRoot}reference/android/support/wearable/watchface/package-summary.html">API reference 81documentation</a> for the Wearable Support Library.</p> 82 83<h3 id="LibEclipse">Download the Wearable Support Library for Eclipse ADT</h3> 84 85<p>If you are using the ADT plugin for Eclipse, download the 86<a href="{@docRoot}shareables/training/wearable-support-lib.zip">Wearable Support Library</a> and 87include it as a dependency in your project.</p> 88 89<h3 id="Permissions">Declare Permissions</h3> 90 91<p>Watch faces require the <code>PROVIDE_BACKGROUND</code> and <code>WAKE_LOCK</code> permissions. 92Add the following permissions to the manifest files of both the wearable app and the mobile 93app under the <code>manifest</code> element:</p> 94 95<pre> 96<manifest ...> 97 <uses-permission 98 android:name="com.google.android.permission.PROVIDE_BACKGROUND" /> 99 <uses-permission 100 android:name="android.permission.WAKE_LOCK" /> 101 ... 102</manifest> 103</pre> 104 105<p class="caution"><strong>Caution:</strong> The handheld app must include all the permissions 106declared in the wearable app.</p> 107 108 109 110<h2 id="CallbackMethods">Implement the Service and Callback Methods</h2> 111 112<p>Watch faces in Android Wear are implemented as 113<a href="{@docRoot}guide/components/services.html">services</a>. 114When a watch face is active, the system invokes the methods in its service when the time changes 115or when an important event occurs (like switching to ambient mode or receiving a new 116notification). The service implementation then draws the watch face on the screen using the 117updated time and any other relevant data.</p> 118 119<p>To implement a watch face, you extend the <code>CanvasWatchFaceService</code> 120and <code>CanvasWatchFaceService.Engine</code> classes, and then you override the callback methods 121in the <code>CanvasWatchFaceService.Engine</code> class. These classes are included in the 122Wearable Support Library.</p> 123 124<p>The following snippet outlines the key methods you need to implement:</p> 125 126<pre> 127public class AnalogWatchFaceService extends CanvasWatchFaceService { 128 129 @Override 130 public Engine onCreateEngine() { 131 /* provide your watch face implementation */ 132 return new Engine(); 133 } 134 135 /* implement service callback methods */ 136 private class Engine extends CanvasWatchFaceService.Engine { 137 138 @Override 139 public void onCreate(SurfaceHolder holder) { 140 super.onCreate(holder); 141 /* initialize your watch face */ 142 } 143 144 @Override 145 public void onPropertiesChanged(Bundle properties) { 146 super.onPropertiesChanged(properties); 147 /* get device features (burn-in, low-bit ambient) */ 148 } 149 150 @Override 151 public void onTimeTick() { 152 super.onTimeTick(); 153 /* the time changed */ 154 } 155 156 @Override 157 public void onAmbientModeChanged(boolean inAmbientMode) { 158 super.onAmbientModeChanged(inAmbientMode); 159 /* the wearable switched between modes */ 160 } 161 162 @Override 163 public void onDraw(Canvas canvas, Rect bounds) { 164 /* draw your watch face */ 165 } 166 167 @Override 168 public void onVisibilityChanged(boolean visible) { 169 super.onVisibilityChanged(visible); 170 /* the watch face became visible or invisible */ 171 } 172 } 173} 174</pre> 175 176<p class="note"><strong>Note:</strong> The <em>WatchFace</em> sample in the Android SDK 177demonstrates how to implement analog and digital watch faces extending the 178<code>CanvasWatchFaceService</code> class. This sample is located in the 179<code>android-sdk/samples/android-21/wearable/WatchFace</code> directory.</p> 180 181<p>The <code>CanvasWatchFaceService</code> class provides an invalidate mechanism similar to 182the {@link android.view.View#invalidate View.invalidate()} method. You can call the 183<code>invalidate()</code> method throughout your implementation when you want the system to 184redraw the watch face. You can only use the <code>invalidate()</code> method in the main UI 185thread. To invalidate the canvas from another thread, call the <code>postInvalidate()</code> 186method.</p> 187 188<p>For more information about implementing the methods in the 189<code>CanvasWatchFaceService.Engine</code> class, see <a 190href="{@docRoot}training/wearables/watch-faces/drawing.html">Drawing Watch Faces</a>.</p> 191 192 193 194<h2 id="RegisterService">Register the Watch Face Service</h2> 195 196<p>After you implement the watch face service, you register the implementation in the manifest 197file of the wearable app. When users install this app, the system uses the information about 198the service to make the watch face available in the <a 199href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android 200Wear companion app</a> and in the watch face picker on the wearable device.</p> 201 202</p>The following snippet shows how to register a watch face implementation 203under the <a href="{@docRoot}guide/topics/manifest/application-element.html"> 204<code>application</code></a> element:</p> 205 206<pre> 207<service 208 android:name=".AnalogWatchFaceService" 209 android:label="@string/analog_name" 210 android:allowEmbedded="true" 211 android:taskAffinity="" 212 android:permission="android.permission.BIND_WALLPAPER" > 213 <meta-data 214 android:name="android.service.wallpaper" 215 android:resource="@xml/watch_face" /> 216 <meta-data 217 android:name="com.google.android.wearable.watchface.preview" 218 android:resource="@drawable/preview_analog" /> 219 <meta-data 220 android:name="com.google.android.wearable.watchface.preview_circular" 221 android:resource="@drawable/preview_analog_circular" /> 222 <intent-filter> 223 <action android:name="android.service.wallpaper.WallpaperService" /> 224 <category 225 android:name= 226 "com.google.android.wearable.watchface.category.WATCH_FACE" /> 227 </intent-filter> 228</service> 229</pre> 230 231<p>The <a 232href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">Android 233Wear companion app</a> and the watch face picker on the wearable device use the preview image 234defined by the <code>com.google.android.wearable.watchface.preview</code> metadata entry when 235presenting users with all the watch faces installed on the device. To obtain this drawable, 236run the watch face on your Android Wear device or in an emulator instance and <a 237href="{@docRoot}sdk/installing/studio-debug.html#screenCap">take a screenshot</a>. On Android Wear 238devices with hdpi screens, the preview image is typically 320x320 pixels in size.</p> 239 240<p>Watch faces that look substantially different on round devices can provide both round and 241square preview images. To specify a round preview image, use the 242<code>com.google.android.wearable.watchface.preview_circular</code> metadata entry. If a watch 243face includes both preview images, the companion app and the watch face picker on the wearable 244show the appropriate one, depending on the shape of the watch. If a round preview image is not 245included, the square preview image is used for both square and round devices. For round devices, 246a square preview image is cropped using a circular shape.</p> 247 248<p>The <code>android.service.wallpaper</code> metadata entry specifies the 249<code>watch_face.xml</code> resource file, which contains a <code>wallpaper</code> 250element:</p> 251 252<pre> 253<?xml version="1.0" encoding="UTF-8"?> 254<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" /> 255</pre> 256 257<p>Your wearable app can contain more than one watch face. You must add a service entry to the 258manifest file of the wearable app for each of your watch face implementations.</p> 259