Use PreferenceActivity to configure widget, how retrieve preference?
i'm using PreferenceActivity class to configure my widget. PreferenceActivity class automatically saves user preferences, so in widget update service i can call context.getSharedPreferences() and i obtain user preferences. Problem is follow: if you have many widget of same type, how PreferenceActivity class 开发者_Go百科saves prefs? how i can load appWidgetId specific prefs from sharedPreferences?
For AppWidgets and preferences I really liked this tutorial
http://www.helloandroid.com/tutorials/mastering-android-widget-development-part1
http://www.helloandroid.com/tutorials/mastering-android-widget-development-part2
http://www.helloandroid.com/tutorials/mastering-android-widget-development-part3
They give a tutorial where a preference is saved for jointly with the appwidget ID and it is shown how it is retrieved for updating the individual widgets.
I'm not entirely clear on what you're trying to do, but I'll take a stab at it. You use the id of your resource specified in the xml to call findPreference(CharSequence key).
Although this question has been asked a long time ago, here are a few thoughts. I am planning to do the same thing myself.
Premier's question brings two points:
1) the configuration activity is a PreferenceActivity, which means that the user input can't be recovered by using findViewById() on a layout. mportuesisf's answer explain how to use a key attribute.
2) the widget instance must save its own preferences. A way to do it is by using the widget ID in a prefix. See http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleAppWidgetConfigure.html
精彩评论