User Profile

Tech Department QnA

Tech Department

  • 0Followers
  • 0Experience
  • 1Questions
  • Tech Department QnA

    Tech Department started a new conversation SharedPreferences Class in Android 1 years ago ago

    I am trying to use this However that gives me error .

    import android.content.Context;
    import android.content.SharedPreferences;

    public class Pref {

        private static SharedPreferences sharedPrefs;
        static String PREF_NAME = "PREF";


        private static void getInstance(Context context) {
            if (sharedPrefs == null)
                sharedPrefs = context.getApplicationContext().getSharedPreferences(
                        PREF_NAME, Context.MODE_PRIVATE);
        }


        public static void putString(Context context, String key, String value) {

            getInstance(context);

            SharedPreferences.Editor edit = sharedPrefs.edit();
            edit.putString(key, value);
            edit.commit();

        }

     

        public static String getString(Context context, String key) {
            getInstance(context);

            String value = sharedPrefs.getString(key, "");

            return value;
        }


        public static void putInteger(Context context, String key, int value) {

            getInstance(context);

            SharedPreferences.Editor edit = sharedPrefs.edit();
            edit.putInt(key, value);
            edit.commit();

        }

     

        public static int getInteger(Context context, String key) {
            getInstance(context);

            int value = sharedPrefs.getInt(key, 0);

            return value;
        }


        public static void putBoolean(Context context, String key, boolean value) {

            getInstance(context);

            SharedPreferences.Editor edit = sharedPrefs.edit();
            edit.putBoolean(key, value);
            edit.commit();

        }

     

        public static boolean getBoolean(Context context, String key) {
            getInstance(context);

            boolean value = sharedPrefs.getBoolean(key, false);

            return value;
        }

     

        public static void putContactList(Context context, String key, String value) {

            getInstance(context);

            SharedPreferences.Editor edit = sharedPrefs.edit();
            edit.putString(key, value);
            edit.commit();

        }

        public static String getContactList(Context context, String key) {
            getInstance(context);

            String value = sharedPrefs.getString(key, null);

            return value;
        }

    }