如何从ConfigActivity的fragment中一键更新多种类型的appwidgets?
How to update multiple types appwidgets from the fragment of ConfigActivity by pressing one button?
我制作了 2 种不同类型的应用小部件来显示一些日历数据。
每种类型都有自己的布局,AppWidgetProvider class,AppWidgetProviderInfo 对象,但它们都基于一个布局使用相同的配置 Activity。
- 第一种:WidgetTodayProvider。
- 第二种:WidgetPeriodProvider
配置内部 Activity 有一个带有“开始”按钮的片段。此按钮必须首次启动选定的 appwidget + 更新 appwidget,因为(我知道)这是配置 activity 首次更新小部件的责任。
目前我有按钮 START 的代码,它只能更新一种类型的小部件 (WidgetTodayProvider):
buttonStart.setOnClickListener {
val intent = Intent(
AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
WidgetTodayProvider::class.java
)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf((activity as ConfigActivity).appWidgetId))
activity?.sendBroadcast(intent)
activity?.setResult(RESULT_OK, (activity as ConfigActivity).resultValue)
activity?.finish()
}
但我不知道如何为按钮 START 编写代码,它会根据用户选择的应用程序小部件(WidgetTodayProvider 或 WidgetPeriodProvider)更新两种类型的小部件...
如何在 START 按钮的代码中定义选定的小部件类型,并根据用户的选择更新此类小部件?
我已经解决了这个问题。
在 ConfigActivity 的片段中:
val appWidgetManager = AppWidgetManager.getInstance(context)
// We have a single ConfigActivity for all the types of the widgets,
// thus we have to differ widget types.
// Get string with short name of widget provider (looks like ".WidgetTodayProvider"),
// using getAppWidgetInfo.provider.shortClassName.
// Delete dot from the string, using substring(1).
val widgetProviderShortName =
appWidgetManager.
getAppWidgetInfo((activity as ConfigActivity).appWidgetId)
.provider.shortClassName
.substring(1)
开始按钮的代码取决于 AppWidgetProvider 的名称:
// Button Start (Start Widget)
buttonStart.setOnClickListener {
// Update appwidget from ConfigActivity
when(widgetProviderShortName) {
"WidgetTodayProvider" ->
intent = Intent(
AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
WidgetTodayProvider::class.java
)
"WidgetPeriodProvider" ->
intent = Intent(
AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
WidgetPeriodProvider::class.java
)
}
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, (activity as ConfigActivity).appWidgetId)
activity?.sendBroadcast(intent)
activity?.setResult(RESULT_OK, (activity as ConfigActivity).resultValue)
activity?.finish()
}
我制作了 2 种不同类型的应用小部件来显示一些日历数据。
每种类型都有自己的布局,AppWidgetProvider class,AppWidgetProviderInfo 对象,但它们都基于一个布局使用相同的配置 Activity。
- 第一种:WidgetTodayProvider。
- 第二种:WidgetPeriodProvider
配置内部 Activity 有一个带有“开始”按钮的片段。此按钮必须首次启动选定的 appwidget + 更新 appwidget,因为(我知道)这是配置 activity 首次更新小部件的责任。
目前我有按钮 START 的代码,它只能更新一种类型的小部件 (WidgetTodayProvider):
buttonStart.setOnClickListener {
val intent = Intent(
AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
WidgetTodayProvider::class.java
)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf((activity as ConfigActivity).appWidgetId))
activity?.sendBroadcast(intent)
activity?.setResult(RESULT_OK, (activity as ConfigActivity).resultValue)
activity?.finish()
}
但我不知道如何为按钮 START 编写代码,它会根据用户选择的应用程序小部件(WidgetTodayProvider 或 WidgetPeriodProvider)更新两种类型的小部件...
如何在 START 按钮的代码中定义选定的小部件类型,并根据用户的选择更新此类小部件?
我已经解决了这个问题。 在 ConfigActivity 的片段中:
val appWidgetManager = AppWidgetManager.getInstance(context)
// We have a single ConfigActivity for all the types of the widgets,
// thus we have to differ widget types.
// Get string with short name of widget provider (looks like ".WidgetTodayProvider"),
// using getAppWidgetInfo.provider.shortClassName.
// Delete dot from the string, using substring(1).
val widgetProviderShortName =
appWidgetManager.
getAppWidgetInfo((activity as ConfigActivity).appWidgetId)
.provider.shortClassName
.substring(1)
开始按钮的代码取决于 AppWidgetProvider 的名称:
// Button Start (Start Widget)
buttonStart.setOnClickListener {
// Update appwidget from ConfigActivity
when(widgetProviderShortName) {
"WidgetTodayProvider" ->
intent = Intent(
AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
WidgetTodayProvider::class.java
)
"WidgetPeriodProvider" ->
intent = Intent(
AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
WidgetPeriodProvider::class.java
)
}
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, (activity as ConfigActivity).appWidgetId)
activity?.sendBroadcast(intent)
activity?.setResult(RESULT_OK, (activity as ConfigActivity).resultValue)
activity?.finish()
}