SharedPreferences application context vs activity context

It is worth reviewing the sources that show that a Context instance (be it an Activity or an Application instance) share the same static map HashMap<String, SharedPreferencesImpl>. So whenever you request an instance of SharedPreferences by the same name via Context.getSharedPreferences(name, mode) you get the same instance since it first checks if the map already … Read more

Put and get String array from shared preferences

You can create your own String representation of the array like this: StringBuilder sb = new StringBuilder(); for (int i = 0; i < playlists.length; i++) { sb.append(playlists[i]).append(“,”); } prefsEditor.putString(PLAYLISTS, sb.toString()); Then when you get the String from SharedPreferences simply parse it like this: String[] playlists = playlist.split(“,”); This should do the job.

Android getDefaultSharedPreferences

Try it this way: final String eulaKey = “mykey”; Context mContext = getApplicationContext(); mPrefs = mContext.getSharedPreferences(“myAppPrefs”, Context.MODE_PRIVATE); SharedPreferences.Editor editor = mPrefs.edit(); editor.putBoolean(eulaKey, true); editor.commit(); in which case you can specify your own preferences file name (myAppPrefs) and can control access persmission to it. Other operating modes include: MODE_WORLD_READABLE MODE_WORLD_WRITEABLE MODE_MULTI_PROCESS

How to Secure Android Shared Preferences?

UPDATED ANSWER: Android has released a security library with EncryptedSharedPreferences in their Jetpack library. Edit: With version v1.1.0 you can support Lollipop (API level 21) and above String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC); SharedPreferences sharedPreferences = EncryptedSharedPreferences.create( “secret_shared_prefs”, masterKeyAlias, context, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ); // use the shared preferences and editor as you normally would SharedPreferences.Editor editor = … Read more

How to save and retrieve Date in SharedPreferences

To save and load accurate date, you could use the long (number) representation of a Date object. Example: //getting the current time in milliseconds, and creating a Date object from it: Date date = new Date(System.currentTimeMillis()); //or simply new Date(); //converting it back to a milliseconds representation: long millis = date.getTime(); You can use this … Read more

onSharedPreferenceChanged not fired if change occurs in separate activity?

The OnSharedPreferenceChangeListener gets garbage collected in your case if you use an anonymous class. To solve that problem use the following code in PreferenceActivity to register and unregister a change listener: public class MyActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener { @Override protected void onResume() { super.onResume(); // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences() … Read more

How to detect if changes were made in the preferences?

Do SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // your stuff here } }; In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method. That’s what I do and I never have a problem. Else it’s your conditional … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)