Jetpack 架构组件学习——使用 Glance 实现桌面小组件
作者:不在线第一只蜗牛
- 2025-01-23 福建
本文字数:3211 字
阅读完需:约 11 分钟
基本使用
1.添加依赖
添加 Glance 依赖:
// For AppWidgets support
implementation "androidx.glance:glance-appwidget:1.1.0"
// For interop APIs with Material 3
implementation "androidx.glance:glance-material3:1.1.0"
// For interop APIs with Material 2
implementation "androidx.glance:glance-material:1.1.0"
复制代码
2.正常的相关设置
xml 文件夹添加小组件widget_info.xml
配置:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:targetCellWidth="2"
android:targetCellHeight="2"
android:minWidth="250dp"
android:minHeight="250dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/glance_default_loading_layout"
tools:targetApi="s">
</appwidget-provider>
复制代码
清单文件配置:
<receiver android:name=".appwidgets.MyAppWidgetReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
复制代码
3.创建 widget 对象和实现界面
需要注意的几点:
1、界面的 Row,Column,Text 等,注意要使用 Glance 的包下的,否则会无法正常显示界面
官方也是说明了,Row,Box,Column 实际最后创建的对象对应的 Linearlayout 和 Framelayout
2、点击跳转的对应 actionStartActivity 方法,实际是返回一个 Action 对象 通过GlanceModifier.clickable(actionStartActivity(MainActivity::class.java))
进行对应跳转
除了 activity 外,还能启动 Service,BoardCast,详见此文档处理用户互动 | Jetpack Compose | Android Developers
3、关于背景圆角的方法,目前采用 Box 里面套一个 Image 和具体内容实现,Image 去加载我们 drawable 里的矢量图对象(shape 的那种)来实现
4、remember 在里面可用,但如果结合 animateColorAsState 这种动画效果,实际上并没有效果(只是单纯的变更,没有中间过渡过程)
class MyAppWidgetReceiver : GlanceAppWidgetReceiver() {
//MyAppWidget里就是类似我们remoteview的创建
override val glanceAppWidget: GlanceAppWidget = MyAppWidget()
}
class MyAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
// In this method, load data needed to render the AppWidget.
// Use `withContext` to switch to another thread for long running
// operations.
provideContent {
val upSpeed by DataRespotiy.upSpeed.asFlow().collectAsState(initial = 0L)
val downloadSpeed by DataRespotiy.downloadSpeed.asFlow().collectAsState(initial = 0L)
Box(GlanceModifier.fillMaxSize()) {
Image(provider = ImageProvider(R.drawable.app_widget_bg), contentDescription = null, modifier = GlanceModifier.fillMaxSize())
Column(GlanceModifier.fillMaxSize()) {
Spacer(GlanceModifier.height(28.dp))
Row(GlanceModifier.fillMaxWidth().padding(horizontal = 20.dp)) {
androidx.glance.Image(provider = ImageProvider(R.mipmap.logo), contentDescription = null, modifier = GlanceModifier.size(108.dp))
Spacer(modifier = GlanceModifier.width(24.dp))
Column(GlanceModifier.defaultWeight()) {
Row(modifier = GlanceModifier.wrapContentHeight(), horizontalAlignment = Alignment.End, verticalAlignment = Alignment.Vertical.CenterVertically) {
Text(text = "${upSpeed} Kb/s", style = TextStyle(color = ColorProvider(APP_Primary_color), fontSize = 22.sp))
Spacer(modifier = GlanceModifier.width(4.dp))
androidx.glance.Image(provider = ImageProvider(R.drawable.icon_upload_widget), contentDescription = null, modifier = GlanceModifier.size(32.dp))
}
Spacer(modifier = GlanceModifier.height(18.dp))
Row(modifier = GlanceModifier, horizontalAlignment = Alignment.End, verticalAlignment = Alignment.Vertical.CenterVertically) {
Text(text = "${downloadSpeed} Kb/s", style = TextStyle(color = ColorProvider(APP_Bigfile_color), fontSize = 22.sp))
Spacer(modifier = GlanceModifier.width(4.dp))
androidx.glance.Image(provider = ImageProvider(R.drawable.icon_down_widget), contentDescription = null, modifier = GlanceModifier.size(32.dp))
}
}
}
Spacer(GlanceModifier.height(28.dp))
Box(modifier = GlanceModifier.fillMaxWidth().defaultWeight()) {
Image(provider = ImageProvider(R.drawable.app_widget_bg1), contentDescription = null, modifier = GlanceModifier.fillMaxSize())
Row(
modifier = GlanceModifier.fillMaxSize()
.padding(horizontal = 20.dp,16.dp), horizontalAlignment = Alignment.CenterHorizontally
) {
val modifier = GlanceModifier.defaultWeight()
//todo 不同的item类型
repeat(3) { index ->
Row(modifier, horizontalAlignment = Alignment.CenterHorizontally, verticalAlignment = Alignment.Vertical.CenterVertically) {
androidx.glance.Image(
provider = ImageProvider(R.drawable.icon_net_test), contentDescription = null, modifier = GlanceModifier
.size(108.dp)
.clickable(actionStartActivity(MainActivity::class.java))
)
}
Spacer(GlanceModifier.width(20.dp))
}
}
}
}
}
}
}
}
复制代码
其他补充
更新小组件方法:
val manager = GlanceAppWidgetManager(application)
val widget = MyAppWidget()
val glanceIds = manager.getGlanceIds(widget.javaClass)
glanceIds.forEach { glanceId ->
widget.update(application, glanceId)
}
//第二种方式(实际上和上面的方法是一样的,下面这个是官方给我们封装的另外个方法)
MyAppWidget().updateAll(application)
复制代码
文章转载自:Stars-one
划线
评论
复制
发布于: 刚刚阅读数: 3
不在线第一只蜗牛
关注
还未添加个人签名 2023-06-19 加入
还未添加个人简介
评论