Add several appWidgets with different configuratio

2019-02-15 06:39发布

问题:

I've created a widget which displays a simple textview, which is editable as an Edittext in a configuration activity. I save the inputted text with shared preferences, so the user can tap the widget to edit the text, and the already inputted text appears in the edittextfield. My problem is this. I would like the user to be able to add multiple widgets, but when a seccond widget is added, the same text as in the other widget is loaded from shared preferences. And, when on widget is edited so is the other one. Hope i'm being clear. I kinda have an idea it has something to do with appWidgetIds but i cannot figure it out.

Here's my code, a bit simplified.

public class WidgetConfig extends Activity implements OnClickListener, OnItemSelectedListener {


    AppWidgetManager awm;
    int awID;
    Context c;
    EditText info;
    Button b;
    String note;
    int styleStart = -1, cursorLoc = 0;
    SharedPreferences sp;
    Spinner spinner;
    String[] paths = { "10", "20", "30" };
    File path = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widgetconfig);

        c = WidgetConfig.this;
        info = (EditText)findViewById(R.id.etwidgetconfig);

        ...

        b = (Button)findViewById(R.id.bwidgetconfig);
        loadPrefs();
        b.setOnClickListener(this);

        //Getting Info about the widget that launched this activity
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        if (extras != null){
            awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID );
        }

        awm = AppWidgetManager.getInstance(c);

    }

        ...


    private void loadPrefs(){
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        note = sp.getString("NOTE", "DEFAULT");

        info.setText(note);

    }

    private void savePrefs(String key, String value){
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sp.edit();        
        editor.putString(key, value); // value to store
        editor.commit();   

    }


    public void onClick(View v) {
        // TODO Auto-generated method stub

        savePrefs("NOTE", info.getText().toString());

        RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.tvConfigInput, info.getText());

        ComponentName thisWidget = new ComponentName(this, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, views);

        Intent in = new Intent(c, WidgetConfig.class);
        PendingIntent pi = PendingIntent.getActivity(c, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);


        views.setOnClickPendingIntent(R.id.B_EditAgain, pi);

        awm.updateAppWidget(awID, views);


        Intent result = new Intent();
        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
        setResult(RESULT_OK, result);
        finish();

    }



}

UPDATE:

So i tried this, but nothings different, still doesn't work.

    private void loadPrefs(){
        sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
          , Context.MODE_PRIVATE);
        note = sp.getString("Note", "");

        info.setText(note);

    }

private void savePrefs(String key, String value){
    sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
              , Context.MODE_PRIVATE);
    Editor editor = sp.edit();
    editor.clear();
    editor.putString("Note", info.getText().toString());
    editor.putString(key, value); // value to store
    editor.commit();   

    }

回答1:

You probably don't want to use getDefaultSharedPreferences here. All widgets share the same default shared preferences, so they will be constantly overwriting each other.

I had the same situation in my own app, so I used a custom preference file for each widget. You can name the preference file with the widgetID, and then each widget will always get it's own unique set of preferences.

In the configuration PreferenceActivity:

this.getPreferenceManager().setSharedPreferencesName(
           "widget" + String.valueOf(mAppWidgetId)
);

This will store all of the PreferenceActivity settings into a preference file named after the widget id.

Then in the widget itself, just retrieve the file corresponding to its id:

preferences = context.getSharedPreferences(
      "widget" + String.valueOf(appWidgetId)
      , Context.MODE_PRIVATE);

And retrieve preferences like normal.