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.
Related
I'm trying to add some API integration to my app and i want to add a way to connect or disconnect from my settings app. So if the token isn't present in Shared Preferences the settings would display "Login to xxx" but if it exits then show "Logout from xxx".
example:
<Preference
android:key="pref_connect_to_xxx"
android:title="login to xxx" >
/>
But if the token is present i want to display
<Preference
android:key="pref_connect_to_xxx"
android:title="logout from xxx" >
/>
Any idea about how to do this?
If I understood correctly what you're trying to do, then the following should solve your issue:
if (tokenAlreadySet()) {
Preference connectToXXX = (Preference) findPreference("pref_connect_to_xxx");
connectToXXX.setSummary("logout from xxx");
}
Modify the summary programmatically by finding the preference by its key and, depending on the token being set or not in your SharedPreferences, show one text or another in the summary.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean token = prefs.getInt("token",-1) == -1?false:true;
Preference pref = (Preference) findPreference("pref_connect_to_xxx");
pref.setSummary(token?"Logout":"Login");
If I understand correctly, you are saving a token to the shared preferences, and if it isn't there, you want to treat the user as not logged in?
Checkout the documentation on SharedPreferences: http://developer.android.com/reference/android/content/SharedPreferences.html#getString(java.lang.String, java.lang.String)
Most get methods on the object, including getString has a default value. So you could do: String token = getString(tokenKey, ""); and then check it like: if(token.isEmpty())...
I've implemented a OnPreferenceChangeListener on two preference object in my preference page ( extends PreferenceActivity)
But ever since the preference value isn't updated upon change,
I even tried using :
SharedPreferences sharedPreferences = getSharedPreferences("myCustomSharedPrefs",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(preference.getKey(), Integer.parseInt(newValue.toString()));
editor.commit();
Where preference is the changed preference and newValue is the new value...
What am I missing ?
Thanks
You need to return true to get the value updated, see the Android documentation.
Can you see if preference.getKey() is giving the correct name? Can you print in your console and see. Code looks clean to me. May be the name is different than what is expected.
Also make sure that the value is also having the correct 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
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.
I have a preferences activity with a checkbox "Enable Service".
I read the value like this :
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(con);
ServiceEnabled_Pref = prefs.getBoolean("EnableService", true);
ok, but how can I set this Preference ?
this is not a CustomShared Preference, It is a DefaultShared Preferences and it seems that there is not a method putBoolean for DefaultSharedPreferences.
I need this, cause I have a Widget with a button that needs to set this value to true/false
programatically:
prefs.editor().putBoolean("EnableService", true).commit();
however your best bet for defining the preferenceActivity and its defaults is via an xml file as decribed here with the android:defaultValue attribute
Use the shared preferences editor:
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html