Handling multiple instances of appwidget

风流意气都作罢 提交于 2020-01-22 10:29:34

问题


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.



来源:https://stackoverflow.com/questions/14582395/handling-multiple-instances-of-appwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!