Handling multiple instances of appwidget

前端 未结 1 1330
别跟我提以往
别跟我提以往 2021-02-06 16:08

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

1条回答
  •  無奈伤痛
    2021-02-06 16:37

    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.

    0 讨论(0)
提交回复
热议问题