Pages

May 31, 2012

Example: Shared Preference Getter/Setter


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

public class Util {

 private static String tag="util";
 private static String KP_PREF="kp_pref";
 private SharedPreferences mSharedPref;
 
 public static int getIntValue(Context ctx,String key,int defval)
 {
  SharedPreferences pref = ctx.getSharedPreferences(KP_PREF,Context.MODE_PRIVATE);
  int usageCount = pref.getInt(key, defval);
  return usageCount;
 }
 
 public static void putIntValue(Context ctx,String key,int defval)
 {
  SharedPreferences pref = ctx.getSharedPreferences(KP_PREF,Context.MODE_PRIVATE);
  pref.edit().putInt(key,defval).commit();  
 }
 
}