finish When Expired : SharedPreferences « Core Class « Android






finish When Expired

     
//package org.anddev.andengine.util;

import java.util.GregorianCalendar;

import android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
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 22:43:32 - 02.11.2010
 */
public class BetaUtils {

  private static final String PREFERENCES_BETAUTILS_ID = "preferences.betautils.lastuse";
  //          
  // Getter & Setter
  //          

  //          
  // Methods from SuperClass/Interfaces
  //          

  //          
  // Methods
  //          

  public static boolean finishWhenExpired(final Activity pActivity, final GregorianCalendar pExpirationDate, final int pTitleResourceID, final int pMessageResourceID) {
    return BetaUtils.finishWhenExpired(pActivity, pExpirationDate, pTitleResourceID, pMessageResourceID, null, null);
  }

  public static boolean finishWhenExpired(final Activity pActivity, final GregorianCalendar pExpirationDate, final int pTitleResourceID, final int pMessageResourceID, final Intent pOkIntent, final Intent pCancelIntent) {
    final SharedPreferences spref = SimplePreferences.getInstance(pActivity);

    final long now = System.currentTimeMillis();
    final long lastuse = Math.max(now, spref.getLong(PREFERENCES_BETAUTILS_ID, -1));
    spref.edit().putLong(PREFERENCES_BETAUTILS_ID, lastuse).commit();

    final GregorianCalendar lastuseDate = new GregorianCalendar();
    lastuseDate.setTimeInMillis(lastuse);

    if(lastuseDate.after(pExpirationDate)){
      final Builder alertDialogBuilder = new AlertDialog.Builder(pActivity)
      .setTitle(pTitleResourceID)
      .setIcon(R.drawable.ic_dialog_alert)
      .setMessage(pMessageResourceID);

      alertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
        @Override
        public void onClick(final DialogInterface pDialog, final int pWhich) {
          if(pOkIntent != null) {
            pActivity.startActivity(pOkIntent);
          }
          pActivity.finish();
        }
      });
      alertDialogBuilder.setNegativeButton(android.R.string.cancel, new OnClickListener() {
        @Override
        public void onClick(final DialogInterface pDialog, final int pWhich) {
          if(pCancelIntent != null) {
            pActivity.startActivity(pCancelIntent);
          }
          pActivity.finish();
        }
      })
      .create().show();
      return true;
    }else{
      return false;
    }
  }

  //          
  // Inner and Anonymous Classes
  //          
}
class SimplePreferences  {
  //          
  // Constants
  //          

  //          
  // Fields
  //          

  private static SharedPreferences INSTANCE;
  private static Editor EDITORINSTANCE;

  //          
  // Constructors
  //          

  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;
  }

  //          
  // Getter & Setter
  //          

  //          
  // Methods for/from SuperClass/Interfaces
  //          

  //          
  // Methods
  //          

  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);
  }

  //          
  // Inner and Anonymous Classes
  //          
}

   
    
    
    
    
  








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.Increment Access Count
7.Store your information into SharedPreferences