0

Before I begin, I want to note that I have looked through other questions including this one. I cannot get anything to work right now.

I am trying to set up my widget's preferences; in this case, I have a checkbox preference to either display a percentage or not. In my WidgetConfig class, I have the checkbox preference set up and am trying to use the onBackPressed method to make the widget update.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.new_widget_config);

    final CheckBoxPreference checkBoxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkboxPref");
    checkBoxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {

            SharedPreferences myPreferences = context.getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor prefEditor = myPreferences.edit();

            if (newValue.toString().equals("true"))
            {
                prefEditor.putBoolean("checkboxPref", true);
                prefEditor.commit();
            }
            else
            {
                prefEditor.putBoolean("checkboxPref", false);
                prefEditor.commit();
            }
            return true;
        }
    });
}

@Override
public void onBackPressed() {
    Intent intent=getIntent();
    Bundle extras=intent.getExtras();   
    int widgetId=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    // this is the intent broadcast/returned to the widget
    Intent updateIntent = new Intent(this, BatteryInfoData.class);
    updateIntent.setAction("PreferencesUpdated");
    updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    sendBroadcast(updateIntent);
}

When I try and put the following code into my onReceive method, I get errors that the action cannot be resolved to a variable. Am I doing this right?

    @Override
public void onReceive(Context context, Intent intent) {
    if ("PreferencesUpdated".equals(action)) {
        RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
        int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
        updateWidgetView(context, remoteView, appWidgetId);
    }   
}
Community
  • 1
  • 1
ericcarboni
  • 88
  • 1
  • 13
  • To anyone who is searching for how to update one preference when another changes, also see http://stackoverflow.com/questions/7603633/updating-preferences-in-real-time. – ToolmakerSteve Sep 18 '14 at 01:16

1 Answers1

1

You get resolving issues because you don't have the variable action there. There's a

String action = intent.getAction();

row missing in the begining of your onReceive(Context context, Intent intent).

IBoS
  • 550
  • 5
  • 15