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