桌面组件代码:
class KLNoteWidget : AppWidgetProvider() {
//当用户添加应用微件时会调用此方法,所以它应执行基本设置。
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}
//如果用户添加应用微件的两个实例,只有首次添加时会调用此方法。使用此方法打开一个新的数据库或执行只需要对所有应用微件实例执行一次的其他设置.
override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created
}
//从应用微件托管应用中删除了应用微件的最后一个实例时,会调用此方法。使用此方法来清理在 onEnabled(Context) 中完成的所有工作。
override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
}
override fun onReceive(context: Context?, intent: Intent?) {
super.onReceive(context, intent)
context?.let {
Log.i("KLNoteWidget", "onReceive")
val remoteViews = RemoteViews(context?.packageName, R.layout.k_l_note_widget)
val appWidgetIds = AppWidgetManager.getInstance(context)
.getAppWidgetIds(
ComponentName(
context,
KLNoteWidget::class.java
)
)
val widgetText = MMKVSerialize.getString("AppWidget")
Log.i("KLNoteWidget", widgetText)
remoteViews.setTextViewText(R.id.appwidget_text, widgetText)
//由AppwidgetManager 处理更新widget
val awm = AppWidgetManager.getInstance(context.applicationContext)
awm.updateAppWidget(appWidgetIds, remoteViews)
}
}
}
internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val widgetText = MMKVSerialize.getString("AppWidget")
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.k_l_note_widget)
views.setTextViewText(R.id.appwidget_text, widgetText)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
桌面组件xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.KnowledgePooling.AppWidget.Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.AppWidgetContainer">
<TextView
android:id="@+id/appwidget_text"
style="@style/Widget.KnowledgePooling.AppWidget.InnerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:contentDescription="@string/appwidget_text"
android:text="@string/appwidget_text"
android:textSize="20sp"
android:textStyle="bold|italic" />
RelativeLayout>
因为桌面组件是个继承广播,因此作为静态广播,需要在清单文件上注册
<receiver
android:name=".widgets.appWidget.KLNoteWidget"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/k_l_note_widget_info" />
receiver>
其中, android:resource=“@xml/k_l_note_widget_info” 是桌面组件在系统设置处的配置项,包括图标和大小控制等。
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_widget_description"
android:initialKeyguardLayout="@layout/k_l_note_widget"
android:initialLayout="@layout/k_l_note_widget"
android:minWidth="300dp"
android:minHeight="50dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />
桌面与组件之间,可以通过服务,也可以通过广播的方式进行交互。这里通过一个页面发送广播做处理
class KLAppWidgetActivity : BaseWzjActivity() {
override fun getLayoutId(): Int {
return R.layout.activity_kl_app_widget
}
override fun initView() {
findViewById<EditText>(R.id.edit_app_widget).apply {
setText(MMKVSerialize.getString("AppWidget"))
onAfterTextChanged {
MMKVSerialize.set("AppWidget", it.toString())
}
}
findViewById<ImageView>(R.id.appwidget_edit).clickNoRepeat {
ToastUtil.showLongToast(this, "已更新")
sendBroadcast(Intent().apply {
action = "android.appwidget.action.APPWIDGET_UPDATE"
})
}
}
override fun initData() {
}
}
嘿,日积月累,假以时日,就会有一个没有广告,占用内存极低,超级便捷,可以让我“为所欲为”的工具APP横空出世,想想就美滋滋~