且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在小工具使用onAppWidgetOptionsChanged()?

更新时间:2022-10-29 17:18:03

  

我想每一个它被调整的时间来更新我的窗口小部件。

酷!

  

我想通了,这是在 onAppWidgetOptionsChanged做()

更准确的说,如果你有正确的行动,你的<意向滤光器> ,在Android 4.1及更高版本的设备,你会发现通过 onAppWidgetOptionsChanged()。

  

但我不能找出如何更新的小部件了。

您更新你的的onupdate更新它以同样的方式()。呼叫 updateAppWidget() AppWidgetManager 与适当的 RemoteViews

  

我试图通过调用一切,我把的OnUpdate()重绘窗口小部件,但它不工作。

它不工作是不是你的症状特别有效的说明。

  

我如何能够利用捆绑?

对于捆绑 newOptions ,你可以找到通过新的尺寸范围:

  newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
 

例如, 包含此示例项目的 AppWidgetProvider ,简单地注入这些值转换为字符串,并使用该更新的TextView 。其结果是这样的:

如何在小工具使用onAppWidgetOptionsChanged()?

I would like to update my widget every time it gets resized. I figured out that this is done in:

onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)

But I can't find out how to update the widget in it. I tried to redraw the widget by calling everything that I put in onUpdate(), but it's not working. How can I make use of the bundle?

I would like to update my widget every time it gets resized.

Cool!

I figured out that this is done in onAppWidgetOptionsChanged()

More accurately, if you have the right actions in your <intent-filter>, on Android 4.1+ devices, you will find out about resize events via onAppWidgetOptionsChanged().

But I can't find out how to update the widget in it.

You update it the same way you update it in onUpdate(). Call updateAppWidget() on the AppWidgetManager with an appropriate RemoteViews.

I tried to redraw the widget by calling everything that I put in onUpdate(), but it's not working.

"it's not working" is not a particularly effective description of your symptoms.

How can I make use of the bundle?

For a Bundle named newOptions, you can find out your new size range via:

newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)

For example, this sample project contains an AppWidgetProvider that simply pours those values into a string and uses that to update a TextView. The result looks like: