I have a configuration activity, a large widget provider and a small widget provider. From configuration activity i save some values in shared preferences. From large and small app widget providers i get those shared preferences. I am not able to give the app widget unique ids and i want to have different shared preferences each time i move on from configuration activity to app widget provider. How can i achieve this.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
To give unique widget ids for each instances of the app widget, you can do as follows:
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(ConfigurationActivity.this);
ComponentName thisAppWidget = new ComponentName(
ConfigurationActivity.this, WidgetProvider.class);
int[] appWidgetIds = appWidgetManager
.getAppWidgetIds(thisAppWidget);
Intent startBroadcast = new Intent(ConfigurationActivity.this,
WidgetProvider.class);
startBroadcast.putParcelableArrayListExtra("list", newsList);
startBroadcast.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
appWidgetIds);
startBroadcast.setAction("android.appwidget.action.APPWIDGET_UPDATE");
sendBroadcast(startService);
In order to save data in unique shared preference, you can do as follows:
public void setWidgetNewsCategory(Context context, String category,
int appWidgetId) {
Editor editor = context.getSharedPreferences(
PREFS_NAME_CATEGORY + String.valueOf(appWidgetId),
Context.MODE_PRIVATE).edit();
editor.putString(PREFS_VALUE_CATEGORY + String.valueOf(appWidgetId),
category);
editor.commit();
}
You can retrieve this shared pref value as follows:
public String getWidgetNewsCategory(Context context, int appWidgetId) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
PREFS_NAME_CATEGORY + String.valueOf(appWidgetId),
Context.MODE_PRIVATE);
return sharedPreferences.getString(
PREFS_VALUE_CATEGORY + String.valueOf(appWidgetId), null);
}
And finally,in Widget Provider's onReceive method, do as follows:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
Bundle extras = intent.getExtras();
if (extras != null) {
int[] appWidgetIds = extras
.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds.length > 0) {
this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);//here you can call onUpdate method, and update your views as you wish
}
}
} else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
Bundle extras = intent.getExtras();
if (extras != null
&& extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
final int appWidgetId = extras
.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
this.onDeleted(context, new int[] { appWidgetId });
}
} else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
this.onEnabled(context);
} else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
this.onDisabled(context);
}
}
This is just general solution which has worked for me. Hopefully it will work for you as well.