صفحه ساعت سرویسی است که در یک برنامه Wear OS بسته بندی شده است. وقتی کاربر یک صفحه ساعت موجود را انتخاب میکند، صفحه ساعت نمایش داده میشود و روشهای پاسخ به تماس سرویس فراخوانی میشوند.
وقتی کاربر یک برنامه Wear را نصب میکند که دارای صفحههای ساعت است، صفحههای ساعت با استفاده از انتخابگر صفحه ساعت روی ساعت در دسترس هستند. همچنین، کاربر می تواند یک صفحه ساعت را از یک برنامه همراه در تلفن جفت شده انتخاب کند.
این صفحه نحوه پیکربندی یک پروژه Wear OS را برای گنجاندن چهرههای ساعت و نحوه پیادهسازی سرویس واچ فیس توضیح میدهد.
یک پروژه واچ فیس ایجاد کنید
توجه: توصیه میکنیم از Android Studio برای توسعه سیستمعامل Wear OS استفاده کنید، زیرا راهاندازی پروژه، گنجاندن کتابخانه، و سهولتهای بستهبندی را فراهم میکند.
مراحل زیر را برای ایجاد یک پروژه در Android Studio برای صفحه ساعت خود انجام دهید:
- روی فایل > جدید > پروژه جدید کلیک کنید.
- در پنجره Select a project template ، تب Wear را انتخاب کنید، سپس Watch Face را از لیست گزینه ها انتخاب کنید و روی Next کلیک کنید.
- در پنجره Configure your project ، مقادیر پیش فرض را بپذیرید و روی Finish کلیک کنید.
Android Studio یک پروژه با یک ماژول app
برای سرویس صفحه ساعت شما ایجاد می کند.
وابستگی ها
Android Studio به طور خودکار وابستگی های مورد نیاز را در فایل های build.gradle
شما اضافه می کند. در وابستگی ها کتابخانه چهره ساعت AndroidX گنجانده شده است. برای جزئیات بیشتر در مورد این کتابخانه به نمونه کد در GitHub مراجعه کنید.
مرجع API کتابخانه پشتیبانی پوشیدنی
مستندات مرجع اطلاعات دقیقی در مورد کلاس هایی که برای پیاده سازی واچ فیس استفاده می کنید ارائه می دهد. اسناد مرجع API را برای کتابخانه پشتیبانی Wearable مرور کنید.
مجوزها را اعلام کنید
صفحه ساعت به مجوز WAKE_LOCK
نیاز دارد. مجوز زیر را به فایلهای مانیفست برنامه Wear OS و برنامه تلفن همراه در زیر عنصر manifest
اضافه کنید:
<manifest ...>
<uses-permission
android:name="android.permission.WAKE_LOCK" />
<!-- Required for complications to receive complication data and open the provider chooser. -->
<uses-permission
android:name="com.google.android.wearable.permission.RECEIVE_COMPLICATION_DATA"/>
...
</manifest>
پشتیبانی از مستقیم بوت
شما باید قبل از باز کردن قفل توسط کاربر، با دنبال کردن دستورالعملهای Direct Boot ، صفحه ساعت خود را در دسترس قرار دهید:
- ویژگی
android:directBootAware
را برای سرویس خود در مانیفست خود روی true
تنظیم کنید. - صفحه ساعت شما باید اطلاعات را در حافظه رمزگذاری شده دستگاه ذخیره کند.
روش های سرویس و برگشت تماس را پیاده سازی کنید
واچ فیس ها در Wear OS به عنوان WatchFaceService
پیاده سازی می شوند. پیادهسازی WatchFaceService
به ایجاد سه شی نیاز دارد: UserStyleSchema
، ComplicationSlotsManager
و WatchFace
.
این سه شی با نادیده گرفتن سه روش انتزاعی از WatchFaceService
مشخص می شوند که در مثال زیر نشان داده شده است:
کاتلین
class CustomWatchFaceService : WatchFaceService() {
/**
* The specification of settings the watch face supports.
* This is similar to a database schema.
*/
override fun createUserStyleSchema(): UserStyleSchema = // ...
/**
* The complication slot configuration for the watchface.
*/
override fun createComplicationSlotsManager(
currentUserStyleRepository: CurrentUserStyleRepository
): ComplicationSlotsManager = // ...
/**
* The watch face itself, which includes the renderer for drawing.
*/
override suspend fun createWatchFace(
surfaceHolder: SurfaceHolder,
watchState: WatchState,
complicationSlotsManager: ComplicationSlotsManager,
currentUserStyleRepository: CurrentUserStyleRepository
): WatchFace = // ...
}
ثبت سرویس چهره ساعت
پس از پیاده سازی سرویس واچ فیس، پیاده سازی را در فایل مانیفست اپلیکیشن پوشیدنی ثبت کنید. وقتی کاربران این برنامه را نصب میکنند، سیستم از اطلاعات مربوط به سرویس استفاده میکند تا صفحه ساعت را در برنامه همراه Wear OS و در انتخابگر چهره ساعت در دستگاه پوشیدنی در دسترس قرار دهد.
نمونه زیر نشان می دهد که چگونه می توان پیاده سازی واچ فیس را در عنصر <application>
ثبت کرد:
<service
android:name=".AnalogWatchFaceService"
android:label="@string/analog_name"
android:permission="android.permission.BIND_WALLPAPER" >
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/watch_face" />
<meta-data
android:name="com.google.android.wearable.watchface.preview_circular"
android:resource="@drawable/preview_analog_circular" />
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
<category
android:name=
"com.google.android.wearable.watchface.category.WATCH_FACE" />
</intent-filter>
</service>
برنامه همراه Wear OS by Google و انتخابگر چهره ساعت در دستگاه پوشیدنی از تصویر پیشنمایش تعریفشده توسط مدخل فوقداده com.google.android.wearable.watchface.preview_circular
هنگام ارائه کاربران با تمام صفحههای ساعت نصب شده در دستگاه استفاده میکنند. برای به دست آوردن این قابل ترسیم، صفحه ساعت را در دستگاه Wear OS خود یا در یک نمونه شبیه ساز اجرا کنید و یک اسکرین شات بگیرید . در دستگاههای Wear با صفحهنمایش hdpi، اندازه تصویر پیشنمایش معمولاً 320x320 پیکسل است.
ورودی فراداده android.service.wallpaper
فایل منبع watch_face.xml
را مشخص می کند که حاوی یک عنصر wallpaper
است، همانطور که در نمونه زیر نشان داده شده است:
<?xml version="1.0" encoding="UTF-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" />
برنامه پوشیدنی شما می تواند بیش از یک صفحه ساعت داشته باشد. برای هر یک از پیادهسازیهای صورت ساعت خود باید یک ورودی سرویس به فایل مانیفست برنامه پوشیدنی اضافه کنید.
به منابع مرتبط زیر مراجعه کنید:
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Build a watch face service\n\nA watch face is a [service](/guide/components/services)\npackaged in a [Wear OS app](/training/wearables/apps).\nWhen a user selects an available watch face, the watch face displays and the\nservice callback methods are invoked.\n\nWhen a user installs a Wear app that has watch faces, the watch\nfaces are available on the watch using the watch face selector.\nAlternatively, the user can select a watch face from a companion app on the paired phone.\n\nThis page describes how to configure a Wear OS project to include watch faces and how\nto implement a watch face service.\n\nCreate a watch face project\n---------------------------\n\n**Note:** We recommend that you use [Android Studio](/studio) for Wear OS development, as\nit provides project setup, library inclusion, and packaging conveniences.\n\nComplete the following steps to\n[create a project](/studio/projects/create-project#StartAProject)\nin Android Studio for your watch face:\n\n1. Click **File** \\\u003e **New** \\\u003e **New project**.\n2. In the **Select a project template** window, select the **Wear** tab, then select **Watch Face** from the list of options and click **Next**.\n3. In the **Configure your project** window, accept the default values and click **Finish**.\n\nAndroid Studio creates a project with an `app` module for your watch face service.\n\n### Dependencies\n\nAndroid Studio automatically adds the required dependencies in your `build.gradle`\nfiles. Included in the dependencies is the\n[AndroidX\nwatch face library](/reference/kotlin/androidx/wear/watchface/package-summary); see the\n[code sample](https://github.com/android/wear-os-samples/tree/main/WatchFaceKotlin) on GitHub for details about this library.\n\n### Wearable support library API reference\n\nThe reference documentation provides detailed information about the classes you use to\nimplement watch faces. Browse the\n[API reference\ndocumentation](/reference/android/support/wearable/watchface/package-summary) for the Wearable support library.\n\n### Declare permissions\n\nA watch face requires the `WAKE_LOCK` permission.\nAdd the following permission to the manifest files of both the Wear OS app\nand the mobile phone app under the `manifest` element: \n\n```xml\n\u003cmanifest ...\u003e\n \u003cuses-permission\n android:name=\"android.permission.WAKE_LOCK\" /\u003e\n\n \u003c!-- Required for complications to receive complication data and open the provider chooser. --\u003e\n \u003cuses-permission\n android:name=\"com.google.android.wearable.permission.RECEIVE_COMPLICATION_DATA\"/\u003e\n ...\n\u003c/manifest\u003e\n```\n\n### Support direct-boot\n\nYou must make your watchface available before user-unlock by following\n[Direct Boot](https://developer.android.com/training/articles/direct-boot) guidance:\n\n1. Set `android:directBootAware` attribute to `true` for your service in your manifest.\n2. Your watch face should store information in [device encrypted storage](https://developer.android.com/training/articles/direct-boot#access).\n\n\u003cbr /\u003e\n\nImplement the service and callback methods\n------------------------------------------\n\nWatch faces in Wear OS are implemented as a\n[`WatchFaceService`](/reference/kotlin/androidx/wear/watchface/WatchFaceService).\nImplementing a `WatchFaceService` requires creating three objects: a\n`UserStyleSchema`, a `ComplicationSlotsManager`, and a\n`WatchFace`.\n\nThese three objects are specified by overriding three abstract methods from\n`WatchFaceService`, shown in the following example: \n\n### Kotlin\n\n```kotlin\nclass CustomWatchFaceService : WatchFaceService() {\n\n /**\n * The specification of settings the watch face supports.\n * This is similar to a database schema.\n */\n override fun createUserStyleSchema(): UserStyleSchema = // ...\n\n /**\n * The complication slot configuration for the watchface.\n */\n override fun createComplicationSlotsManager(\n currentUserStyleRepository: CurrentUserStyleRepository\n ): ComplicationSlotsManager = // ...\n\n /**\n * The watch face itself, which includes the renderer for drawing.\n */\n override suspend fun createWatchFace(\n surfaceHolder: SurfaceHolder,\n watchState: WatchState,\n complicationSlotsManager: ComplicationSlotsManager,\n currentUserStyleRepository: CurrentUserStyleRepository\n ): WatchFace = // ...\n\n}\n```\n\nRegister the watch face service\n-------------------------------\n\nAfter you implement the watch face service, register the implementation in the manifest\nfile of the wearable app. When users install this app, the system uses the information about\nthe service to make the watch face available in the [Wear OS companion app](https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en) and in the watch face picker on the wearable device.\n\nThe following sample shows how to register a watch face implementation\nunder the [`\u003capplication\u003e`](/guide/topics/manifest/application-element) element: \n\n```xml\n\u003cservice\n android:name=\".AnalogWatchFaceService\"\n android:label=\"@string/analog_name\"\n android:permission=\"android.permission.BIND_WALLPAPER\" \u003e\n \u003cmeta-data\n android:name=\"android.service.wallpaper\"\n android:resource=\"@xml/watch_face\" /\u003e\n \u003cmeta-data\n android:name=\"com.google.android.wearable.watchface.preview_circular\"\n android:resource=\"@drawable/preview_analog_circular\" /\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.service.wallpaper.WallpaperService\" /\u003e\n \u003ccategory\n android:name=\n \"com.google.android.wearable.watchface.category.WATCH_FACE\" /\u003e\n \u003c/intent-filter\u003e\n\u003c/service\u003e\n```\n\nThe Wear OS by Google companion app and the watch face picker on the wearable device use the preview\nimage defined by the `com.google.android.wearable.watchface.preview_circular` metadata entry when\npresenting users with all the watch faces installed on the device. To obtain this drawable,\nrun the watch face on your Wear OS device or in an emulator instance and\n[take a screenshot](/studio/debug/am-screenshot). On Wear\ndevices with hdpi screens, the preview image is typically 320x320 pixels in size.\n\nThe `android.service.wallpaper` metadata entry specifies the\n`watch_face.xml` resource file, which contains a `wallpaper`\nelement, as shown in the following sample: \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cwallpaper xmlns:android=\"http://schemas.android.com/apk/res/android\" /\u003e\n```\n\nYour wearable app can contain more than one watch face. You must add a service entry to the\nmanifest file of the wearable app for each of your watch face implementations.\n\nRelated resources\n-----------------\n\n\nRefer to the following related resources:\n\n- [WatchFaceKotlin sample](https://github.com/android/wear-os-samples/tree/deprecated/WatchFaceKotlin)"]]