Changing CheckBoxPreference value to false - android

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?

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.

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()

Related

Android SwitchPreference not working

I'm trying to retrieve the SwitchPreference's value using SharedPreferences but it isn't working. I'm using SwitchPreference so that user can turn on/off notifications, but it shows notifications no matter whatever the value is.
Here's the code.
NotificationUtils.java
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
if (preferences.getBoolean("notification_key", true)) {
notificationManager.notify(NOTIFICATION_ID + rowId, notBuilder.build());
}
preferences.xml
<SwitchPreference
android:contentDescription="Turn notifications on/off"
android:defaultValue="true"
android:key="notification_key"
android:summaryOff="Off"
android:summaryOn="On"
android:title="Notifications" />
I also have overridden and registered the OnSharedPreferenceChange listener in SettingsFragment.java.
Try replacing
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
with
PreferenceManager.getDefaultSharedPreferences(context);
I believe you're attempting to retrieve "notification_key" from the wrong SharedPreferences, which is why it's always using the default value of true and showing your notification.
Edit: You can check to see whether the SharedPreferences you're using contains the "notification_key" key with the contains() method.
I solved it, actually the value wasn't toggling, in settings screen the Switch was turning off and on but the value remained the default. Solved it by setting the new value in OnPreferenceChangeListener and that worked.

Shared preference is not working with android lockscreen

I have modified the default android lock screen with some tweaks .
I have use a shared preference in the lock screen to show an overlay text when the phone is booting for the first time . I am getting the overlay in first boot and saving the shared preference value to false . And throughout that session i am able to read the value of the shared preference . But when i restart the phone it seems like the shared preference is resetting
private Boolean mShowOverlay;
private final String SHOW_OVERLAY = "showoverlay";
private SharedPreferences myPrefs ;
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
mShowOverlay = myPrefs.getBoolean(SHOW_OVERLAY, true);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putBoolean(SHOW_OVERLAY, false);
prefsEditor.commit();
Are you sure you are committing the SharedPreference, I mean calling commit() method??

Service not saving SharedPreference for Main Activity?

I am using a service to download and retrieve a list of URL's and put them in a sharedpreference.
With this..
SharedPreference images_article = this.getSharedPreferences("images_articles", MODE_WORLD_READABLE);
editor.putString("article2", urlImage2);
editor.putString("article3", urlImage2);
editor.commit();
Then in my Main.Activity i pull the url's from preference.
SharedPreference images_article = this.getSharedPreferences("images_articles", MODE_WORLD_READABLE);
urlImage2 = images_article.getString("article2", "NO ARTICLE AVAILABLE");
urlImage3 = images_article.getString("article3", "NO ARTICLE URL AVAILABLE");
The only problem is for some reason it isnt going inside of the shared preference, because the Main activity is loading the OLD URL's that has now changed. But in the Service i log the url's being retrieved and they are updated but for some reason in the main activity it still loads the old one. and im retrieving them from the same preference.
Is there anything i am missing or a better way to do this? Any help would be great!!
I was running in to a similar issue with my SharedPreferences between my Activity and Service. I ended up not using the default and used my own set file name
in the activity and service I set
private static final String PREFERENCE_NAME = "MyPreferenceFileName";
Then to get the values:
SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
pref_checked = pref.getBoolean("checked", true);
and to set the values:
SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("checked", value);
editor.commit();
This allowed me to use the same get and put logic in both my Service and Activity without any issues. I hope this helped.
This might be happening due to the different contexts you are accessing from. I am not very sure though, but you can try this :
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
Use this code, whenever you try to access SharedPreferences, that is, from both the Service and the Activity. That might solve your problem.
Using this in both the Activity and Service classes seems to work for me, as it should now be using the same Context to access the application SharedPreferences:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

onSharedPreferenceChanged edit stored value?

If I have a onSharedPreferenceChanged event in my PreferenceActivity, that is checking if a CheckBoxPreference is checked or not and setting setEnabled on some other Preference, does the actual value of the other Preference get changed in the SharedPreferences, or do I have to manually set them?
What I have is:
public void onSharedPreferenceChanged(Settings sharedPreferences, String key)
{
CheckBoxPreference cbUpdatesEnabled = (CheckBoxPreference)getPreferenceScreen().findPreference("updatesenabled");
CheckBoxPreference cbVibrate = (CheckBoxPreference)getPreferenceScreen().findPreference("vibrate");
cbVibrate.setEnabled(cbUpdatesEnabled.isChecked());
}
Which toggles whether the "vibrate" checkbox is enabled or disabled in the UI, but then do I also have to use:
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("vibrate", cbUpdatesEnabled.isChecked());
editor.commit();
Or is there some other way of updating the SharedPreferences? It doesn't look like the value is automatically saved just based on if it's enabled or not.
You don't have to manually update the preference value if you are using PreferenceActivity.
For more information check following tutorials.
Click here
Click here

Android: Setting preferences programmatically

I have a small ap with preferences. In this class I've set the onPreferenceClick to get coordinates from the GPS. When the listener returns, my hope was to set the lat / long textedits automatically. I've tried every source sample out there, no luck:
public void onLocationChanged(Location l) {
Log.d("H","Location Received: "+l.toString());
prefLocation.setSummary(l.toString());
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor1 = settings.edit();
editor1.putString("posLat","xxx");
editor1.commit();
}
When this code executes when I click on my PreferenceScreen and the location listener returns, the EditTextPreference with the key "posLat" still shows the old value.
I'm going crazy trying to figure out what's wrong!
My prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="My_Shared_Preferences">
<PreferenceCategory
android:title="Your Location">
<PreferenceScreen
android:title="Find Location..."
android:key="location"
android:summary="Click here to read your location automatically"/>
<EditTextPreference
android:title="Latitude"
android:key="posLat" />
<EditTextPreference
android:title="Longtitude"
android:key="posLong" />
<EditTextPreference
android:title="Altitude"
android:key="posAlt" />
</PreferenceCategory>
</PreferenceScreen>
Alternatively, maybe there is a better way to store the location value for an application? I don't really want the user to manually enter the coordinates, but I dont want to resort to saving and loading a text file with the settings, it seems so crude.
I've been having kind of the same problem. My solution was to use the default shared preferences instead of manually created preferences with a given name.
Change the reference to SharedPreferences from this:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
to this:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
and see if that makes any difference.
Seems that this has actually changed (see 1 and 2)
The new way to do this is
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(PREF_NAME, YOUR_VALUE);
editor.commit();
What is "the old value". With the code that you've posted it looks like the only thing that is going to get stored in your preferences is the String "xxx" you need to replace that with a string that represents the location inside your putString() method.

Categories

Resources