I wouldn't try to "optimize" my code by storing stuff in static
variables unless you have proof that it's causing a performace issue.

I have a PrefUtil class very similar to yours, but it just retrieves
the SharedPreferences object every time.

On Apr 10, 5:56 am, Bob Kerns <[email protected]> wrote:
> Your abuse of global variables (i.e. with the static keyword) is
> giving you memory leaks.
>
> On Apr 9, 9:58 am, Gubatron <[email protected]> wrote:
>
>
>
> > Here's a static wrapper I implemented to get and set properties real
> > easy
>
> > package gubatron.android.util;
>
> > import java.util.Arrays;
>
> > import android.content.Context;
> > import android.content.SharedPreferences;
> > import android.content.SharedPreferences.Editor;
>
> > /**
> >  * Convenience class to get or set preference values without dealing
> > with Android's SharedPreferenes and the editor.
> >  * Licensed under LGPL.
> >  *
> >  * @author gubatron
> >  *
> >  * Examples:
> >  *
> >  * PrefUtils.setString(key, value)
> >  *
> >  * String value = PrefUtils.getString(key);
> >  * PrefUtils.getString(key, defaultValue);
> >  *
> >  *
> >  */
> > public final class PrefUtils {
>
> >         private static SharedPreferences PREFERENCES;
> >         private static Editor EDITOR;
>
> >         static {
> >                 PREFERENCES =
> > GlobalVariables.APP_CONTEXT.getSharedPreferences(GlobalConstants.PREFS_FILE 
> > _NAME,
> > Context.MODE_PRIVATE);
> >                 EDITOR = PREFERENCES.edit();
> >         }
>
> >         public static String getString(String prefKeyName) {
> >                 return getString(prefKeyName, null);
> >         }
>
> >         public static String getString(String prefKeyName, String
> > defaultValue) {
> >                 String result  = PREFERENCES.getString(prefKeyName, 
> > defaultValue);
> >                 if (result == null)
> >                         return null;
> >                 return result;
> >         }
>
> >         public static byte[] getByteArray(String prefKeyName) {
> >                 return getByteArray(prefKeyName, null);
> >         }
>
> >         public static void setString(String prefKeyName, String value) {
> >                 EDITOR.putString(prefKeyName,value);
> >                 EDITOR.commit();
> >         }
>
> >         public static byte[] getByteArray(String prefKeyName, String
> > defaultValue) {
> >                 String result = getString(prefKeyName, defaultValue);
> >                 if (result == null)
> >                         return null;
> >                 return result.getBytes();
> >         }
>
> >         public static void setByteArray(String prefKeyName, byte[] value) {
> >                 EDITOR.putString(prefKeyName,new String(value));
> >                 EDITOR.commit();
> >         }
>
> >         public static boolean getBoolean(String prefKeyName, boolean
> > defaultValue) {
> >                 return PREFERENCES.getBoolean(prefKeyName, defaultValue);
> >         }
>
> >         public static void setBoolean(String prefKeyName, boolean value) {
> >                 EDITOR.putBoolean(prefKeyName,value);
> >                 EDITOR.commit();
> >         }
>
> >         public static int getInt(String prefKeyName, int defaultValue) {
> >                 return PREFERENCES.getInt(prefKeyName, defaultValue);
> >         }
>
> >         public static void setInt(String prefKeyName, int value) {
> >                 EDITOR.putInt(prefKeyName,value);
> >                 EDITOR.commit();
> >         }
>
> >         public static void clearSettings() {
> >                 EDITOR.clear();
> >                 EDITOR.commit();
> >         }
>
> >         public static void testPrefUtils() {
> >                 clearSettings();
> >                 assert(getString(GlobalConstants.PREF_KEY_UUID)==null);
>
> >                 byte[] macAddress =
> > WifiUtils.getWiFiMACAddress(GlobalVariables.APP_CONTEXT);
> >                 PrefUtils.setByteArray(GlobalConstants.PREF_KEY_UUID, 
> > macAddress);
> >                 int macAddressChecksum =
> > ByteUtils.tripleByteArrayToSmallInt(ByteUtils.getByteArrayChecksum(macAddre 
> > ss));
>
> >                 byte[] fetchedMacAddress =
> > PrefUtils.getByteArray(GlobalConstants.PREF_KEY_UUID);
> >                 int fetchedAddressChecksum =
> > ByteUtils.tripleByteArrayToSmallInt(ByteUtils.getByteArrayChecksum(fetchedM 
> > acAddress));
> >                 Arrays.equals(macAddress, fetchedMacAddress);
>
> >                 assert(macAddressChecksum == fetchedAddressChecksum);
>
> >                 PrefUtils.setInt("test.int.key", Integer.MAX_VALUE);
> >                 assert(Integer.MAX_VALUE == 
> > PrefUtils.getInt("test.int.key",0));
>
> >                 PrefUtils.setInt("test.int.key", Integer.MIN_VALUE);
> >                 assert(Integer.MIN_VALUE == 
> > PrefUtils.getInt("test.int.key",0));
>
> >         }
>
> > }
>
> > On Apr 9, 5:47 am, Kim Ras <[email protected]> wrote:
>
> > > Excellent, I looked at that and was not sure if that fas able to share
> > > the settings between Servises and Activities.. I will look again
> > > Thanks
> > > Kim- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to