0

I am currently doing an Android security application and I'm trying to uncheck a CheckboxPreference after certain conditions have been made. So I'm trying to uncheck the checkbox by doing this, by default the CheckBoxPreference is actually false so it is unchecked.

Preferences:

<CheckBoxPreference
            android:id="@+id/isPhysicalTheftEnabled"
            android:key="isPhysicalTheftEnabled"
            android:title="Enable Physical Theft Protection"
            android:summary="Select to enable the Physical Theft Protection"
            android:defaultValue="false"/>
<Preference android:key="physicaltheft" android:title="Set Physical Theft Protection Password" android:dependency="isPhysicalTheftEnabled"></Preference>

SharedPreferences inside Activity:

    SharedPreferences sp = getSharedPreferences("isPhysicalTheftEnabled", MODE_WORLD_READABLE);
    SharedPreferences.Editor ed = sp.edit();
    ed.putBoolean("isPhysicalTheftEnabled", false);
    ed.commit();

The CheckBoxPreference wouldn't be untick even if I have done so. Any idea what could be the problem?

Sa Dec
  • 114
  • 7
dythe
  • 840
  • 5
  • 21
  • 45
  • Consider reloading your preferenceActivity : http://stackoverflow.com/questions/7466189/reload-preferences-in-preferenceactivity-on-resume – Snicolas Jul 30 '12 at 20:46

2 Answers2

1

I dont really understand what you try to accomplish, but my guess is you get the wrong SharedPrefence file.

Try calling

SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(myContext);

Or check what the filename is called, look under data/data/yourpackage/shared_prefs

You write to a preference file called "isPhysicalTheftEnabled" which i guess is not the same.

user1278085
  • 11
  • 1
  • 3
  • ok i checked it out my value isPhysicalTheftEnabled is written to my _preferences.xml, am i able to edit the value of a boolean in that file? – dythe Jul 30 '12 at 22:20
0

Uhh, you're putting a "false" in your shared preferences without touching the checkboxpreference view... if you stored a reference somewhere, great it not

  CheckBoxPreference cbp = (CheckBoxPreference) findViewById(R.id.isPhysicalTheftEnabled);
  SharedPreferences sp = getSharedPreferences("isPhysicalTheftEnabled", MODE_WORLD_READABLE);
  SharedPreferences.Editor ed = sp.edit();
  ed.putBoolean("isPhysicalTheftEnabled", cbp.getChecked());  //puts the real value in here
  ed.commit();
  cbp.setChecked(false);    //turn it off as you intended
  cbp.postInvalidate();    //refresh the view

setChecked() might have to be called from a Handler, or via runOnUIThread()

Shark
  • 6,513
  • 3
  • 28
  • 50
  • Cannot cast from View to CheckBoxPreference, i'm getting this error. – dythe Jul 30 '12 at 20:51
  • oh snap, so whats the actual view that's being ticked and unticked? replace CheckBoxPreference with that and try again. my bad :) please post it's onBindView(View view) method. – Shark Jul 30 '12 at 20:52
  • It's actually a checkboxpreference, doesn't work for some reason – dythe Jul 30 '12 at 20:53
  • maybe you didn't bind a view to it and thats why it doesnt work? i'm kinda blind-guessing now. but in any case, you need to refresh the view as well. probably from a handler as well. – Shark Jul 30 '12 at 20:54
  • I'm trying to place this piece of code inside a button onclicklistener in a onCreate method, could that be the cause? – dythe Jul 30 '12 at 20:55
  • no, if anything it would complain about some fields not being final. if findViewById is giving the error, try fetching the view in onCreate() as final, and then using it inside the onClick() – Shark Jul 30 '12 at 20:57