Increment Access Count : SharedPreferences « Core Class « Android






Increment Access Count

     
//package org.anddev.andengine.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 18:55:12 - 02.08.2010
 */
class SimplePreferences {
  private static SharedPreferences INSTANCE;
  private static Editor EDITORINSTANCE;

  public static SharedPreferences getInstance(final Context pContext) {
    if(SimplePreferences.INSTANCE == null) {
      SimplePreferences.INSTANCE = PreferenceManager.getDefaultSharedPreferences(pContext);
    }
    return SimplePreferences.INSTANCE;
  }

  public static Editor getEditorInstance(final Context pContext) {
    if(SimplePreferences.EDITORINSTANCE == null) {
      SimplePreferences.EDITORINSTANCE = SimplePreferences.getInstance(pContext).edit();
    }
    return SimplePreferences.EDITORINSTANCE;
  }

  public static int incrementAccessCount(final Context pContext, final String pKey) {
    return SimplePreferences.incrementAccessCount(pContext, pKey, 1);
  }

  public static int incrementAccessCount(final Context pContext, final String pKey, final int pIncrement) {
    final SharedPreferences prefs = SimplePreferences.getInstance(pContext);
    final int accessCount = prefs.getInt(pKey, 0);

    final int newAccessCount = accessCount + pIncrement;
    prefs.edit().putInt(pKey, newAccessCount).commit();

    return newAccessCount;
  }

  public static int getAccessCount(final Context pCtx, final String pKey) {
    return SimplePreferences.getInstance(pCtx).getInt(pKey, 0);
  }

}

   
    
    
    
    
  








Related examples in the same category

1.Store information into Preference
2.Edit Preferences
3.Structured Preferences
4.Save value to preference
5.Example that shows finding a preference from the hierarchy and a custom preference type.
6.finish When Expired
7.Store your information into SharedPreferences