Điều kiện tiên quyết và cách thiết lập
Trước khi bắt đầu, hãy đảm bảo môi trường của bạn đáp ứng các yêu cầu sau.
Yêu cầu về thời gian chạy
Tiện ích Wear yêu cầu APK com.google.android.wearable.protolayout.renderer phiên bản 1.6.1 trở lên trên thiết bị mục tiêu.
Tải phiên bản trình kết xuất tương thích theo một trong những cách sau:
- Trình mô phỏng Wear OS 7: Sử dụng hình ảnh trình mô phỏng Wear OS 7. Các phiên bản thấp hơn 7 không phù hợp. Để biết hướng dẫn thiết lập, hãy xem bài viết Thiết lập trình mô phỏng Wear OS 7.
- Thiết bị thực: Sử dụng thiết bị Wear OS thực nhận bản cập nhật tự động từ Cửa hàng Google Play hoặc thiết bị dành cho nhà phát triển đã đăng nhập vào Cửa hàng Google Play.
Để kiểm tra phiên bản bạn đã cài đặt trên thiết bị, hãy sử dụng lệnh sau:
adb shell dumpsys package com.google.android.wearable.protolayout.renderer | \
grep -m 1 versionName | \
awk -F= '{print $2}'
Cấu hình Gradle
Thư viện Tiện ích Wear có trên Google Maven.
1. Định cấu hình phiên bản SDK
Đảm bảo bạn đã đặt compileSdk và targetSdk thành 37 trở lên.
android {
compileSdk = 37
// ...
defaultConfig {
targetSdk = 37
// ...
}
}
2. Thêm phần phụ thuộc
Thêm các phần phụ thuộc sau vào tệp build.gradle.kts của ứng dụng:
Groovy
dependencies { // Core Wear Widget and Remote Compose libraries implementation "androidx.compose.remote:remote-creation-compose:1.0.0-alpha11" implementation "androidx.compose.remote:remote-core:1.0.0-alpha11" implementation "androidx.glance.wear:wear:1.0.0-alpha10" implementation "androidx.glance.wear:wear-core:1.0.0-alpha10" implementation "androidx.wear.compose.remote:remote-material3:1.0.0-alpha04" // Tooling for previews (optional, but recommended) implementation "androidx.compose.remote:remote-tooling-preview:1.0.0-alpha11" implementation "androidx.wear.compose:compose-ui-tooling:1.6.2" implementation "androidx.wear.tiles:tiles-tooling-preview:1.6.0" debugImplementation "androidx.wear.tiles:tiles-renderer:1.6.0" }
Kotlin
dependencies { // Core Wear Widget and Remote Compose libraries implementation("androidx.compose.remote:remote-creation-compose:1.0.0-alpha11") implementation("androidx.compose.remote:remote-core:1.0.0-alpha11") implementation("androidx.glance.wear:wear:1.0.0-alpha10") implementation("androidx.glance.wear:wear-core:1.0.0-alpha10") implementation("androidx.wear.compose.remote:remote-material3:1.0.0-alpha04") // Tooling for previews (optional, but recommended) implementation("androidx.compose.remote:remote-tooling-preview:1.0.0-alpha11") implementation("androidx.wear.compose:compose-ui-tooling:1.6.2") implementation("androidx.wear.tiles:tiles-tooling-preview:1.6.0") debugImplementation("androidx.wear.tiles:tiles-renderer:1.6.0") }
Xây dựng tiện ích Hello World
Tiện ích Wear bao gồm một dịch vụ mở rộng
GlanceWearWidgetService và một lớp tiện ích
mở rộng GlanceWearWidget. Giao diện người dùng được xác định bằng các hàm @RemoteComposable. hàm @RemoteComposable.
Xác định dịch vụ
Dịch vụ là điểm truy cập mà hệ thống liên kết đến.
Để xác định tiện ích, hãy tạo một dịch vụ mở rộng GlanceWearWidgetService.
class HelloWidgetService : GlanceWearWidgetService() { override val widget: GlanceWearWidget = HelloWidget() }
Xác định tiện ích
Lớp tiện ích cung cấp dữ liệu và bố cục cho tiện ích.
class HelloWidget : GlanceWearWidget() { override suspend fun provideWidgetData( context: Context, params: WearWidgetParams, ): WearWidgetData { return WearWidgetDocument(background = WearWidgetBrush.color(Color.Blue.rc)) { HelloWidgetContent() } } }
Xác định nội dung
Nội dung được xây dựng bằng các thành phần Remote Compose.
@RemoteComposable @Composable fun HelloWidgetContent() { RemoteBox( modifier = RemoteModifier.fillMaxSize(), contentAlignment = RemoteAlignment.Center, ) { RemoteText( text = "Hello World".rs, color = Color.White.rc ) } }
Tạo XML cấu hình tiện ích
Tạo tệp mới res/xml/hello_widget_info.xml để xác định các thuộc tính và kích thước được hỗ trợ của tiện ích. Để tham khảo đầy đủ các thuộc tính XML
được hỗ trợ trong thẻ <wearwidget-provider>, hãy xem tài liệu
WearWidgetProviderInfo.
<wearwidget-provider description="@string/hello_widget_description" icon="@mipmap/ic_launcher" label="@string/hello_widget_label" preferredType="SMALL"> <container type="SMALL" previewImage="@drawable/widget_preview_small" /> <container type="LARGE" previewImage="@drawable/widget_preview_large" /> </wearwidget-provider>
Đăng ký trong AndroidManifest.xml
Đăng ký dịch vụ trong AndroidManifest.xml bằng các bộ lọc ý định và siêu dữ liệu bắt buộc.
<service android:name=".snippets.widget.HelloWidgetService" android:exported="true" android:icon="@mipmap/ic_launcher" android:label="@string/hello_widget_label" android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER"> <intent-filter> <action android:name="androidx.glance.wear.action.BIND_WIDGET_PROVIDER" /> <!-- If you already have a Tile, omit the following line. --> <action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" /> </intent-filter> <meta-data android:name="androidx.glance.wear.widget.provider" android:resource="@xml/hello_widget_info" /> <meta-data android:name="androidx.wear.tiles.PREVIEW" android:resource="@drawable/tile_preview" /> </service>
Xây dựng và triển khai
Sau khi xác định dịch vụ và tiện ích, bạn có thể xây dựng dự án và triển khai dự án đó vào một thiết bị hoặc trình mô phỏng.
Xây dựng và cài đặt
Xây dựng dự án và cài đặt APK gỡ lỗi vào thiết bị hoặc trình mô phỏng đã kết nối:
./gradlew :app:installDebug
Thêm và xem trước tiện ích
Sau khi cài đặt ứng dụng, hãy sử dụng adb để thêm tiện ích vào băng chuyền theo phương thức lập trình và hiển thị tiện ích đó trên màn hình.
Lưu ý: Tiện ích Wear sử dụng cơ sở hạ tầng thẻ thông tin cơ bản cho mục đích gỡ lỗi. Do đó, các lệnh adb yêu cầu thao tác
add-tile và show-tile.
1. Thêm tiện ích vào băng chuyền:
adb shell am broadcast \
-a com.google.android.wearable.app.DEBUG_SURFACE \
--es operation add-tile \
--ecn component <your_package_name>/.HelloWidgetService
2. Hiện tiện ích:
adb shell am broadcast \
-a com.google.android.wearable.app.DEBUG_SYSUI \
--es operation show-tile \
--ei index 0
Bản xem trước của Android Studio cũng có sẵn để giúp bạn kiểm thử bố cục trên nhiều kích thước màn hình.