Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

android - How to use onAppWidgetOptionsChanged() in a widget?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

Resize Widget, During Resize Operation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...