Android SharedPreferences - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android SharedPreferences class.

This code example shows how to use the following methods:edit
	public final void remove() { 
		apply(edit().remove(key));
	} 
 
	protected Editor edit() {
		return sharedPreferences.edit();
	} 
 
	protected final void apply(Editor editor) {
		SharedPreferencesCompat.apply(editor);
	} 
 
} 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:contains, edit
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getLong, getString
	} 
	 
	public void load(Context context) {
		if(!loaded) {
			SharedPreferences prefs = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
			c2DMRegistrationId = prefs.getString("c2DMRegistrationId", null);
			registeredUserId = prefs.getLong("registeredUserId", -1L);
			pendingC2DMRequestTime = prefs.getLong("pendingC2DMRequestTime", 0);
			pendingSocializeRequestTime = prefs.getLong("pendingSocializeRequestTime", 0);
			lastC2DMRegistrationTime = prefs.getLong("lastC2DMRegistrationTime", 0);
			lastSocializeRegistrationTime = prefs.getLong("lastSocializeRegistrationTime", 0);
			 
			if(logger != null && logger.isDebugEnabled()) {
				logger.debug("Loaded notification state with registration id [" +
						c2DMRegistrationId +
						"], user id [" + 
						registeredUserId +
						"]"); 
			}		 
		} 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getInt
		GdxTests.tests.add(MatrixTest.class); 
		List<String> testNames = GdxTests.getNames();
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, testNames));
 
		prefs = getSharedPreferences("libgdx-tests", Context.MODE_PRIVATE);
		getListView().setSelectionFromTop(prefs.getInt("index", 0), prefs.getInt("top", 0));
	} 
 
	protected void onListItemClick (ListView listView, View view, int position, long id) {
		super.onListItemClick(listView, view, position, id);
 
		Editor editor = prefs.edit();
		editor.putInt("index", listView.getFirstVisiblePosition());
		editor.putInt("top", listView.getChildAt(0) == null ? 0 : listView.getChildAt(0).getTop());
		editor.commit();
 
		Object o = this.getListAdapter().getItem(position);
		String testName = o.toString();
 
		Bundle bundle = new Bundle();
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:edit, getBoolean, getLong
    private final static int LAUNCHES_UNTIL_PROMPT = 7;
     
    public static void appLaunched(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
 
        if (prefs.getBoolean("dontshowagain", false)) { return ; }
         
        SharedPreferences.Editor editor = prefs.edit();
         
        // Increment launch counter 
        long launch_count = prefs.getLong("launch_count", 0) + 1;
        editor.putLong("launch_count", launch_count);
 
        // Get date of first launch 
        Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
        if (date_firstLaunch == 0) {
            date_firstLaunch = System.currentTimeMillis();
            editor.putLong("date_firstlaunch", date_firstLaunch);
        } 
         

8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getInt, getLong, getString
    } 
  } 
 
  public static String getRegistrationId(Context context) {
    final SharedPreferences prefs=getGCMPreferences(context);
    String registrationId=prefs.getString(PROPERTY_REG_ID, "");
    // check if app was updated; if so, it must clear 
    // registration id to 
    // avoid a race condition if GCM sends a message 
    int oldVersion=
        prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int newVersion=getAppVersion(context);
    if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) {
      Log.v(TAG, "App version changed from " + oldVersion + " to "
          + newVersion + "; resetting registration id");
      clearRegistrationId(context);
      registrationId="";
    } 
    else if (isRegistrationExpired(context)) {
      Log.v(TAG, "Registration expired; resetting registration id");

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:edit
public abstract class EditorHelper<T extends EditorHelper<T>> { 
 
	private final Editor editor;
 
	public EditorHelper(SharedPreferences sharedPreferences) {
		editor = sharedPreferences.edit();
	} 
 
	protected Editor getEditor() {
		return editor;
	} 
 
	public final T clear() { 
		editor.clear();
		return cast(); 
	} 
 
	public final void apply() { 
		SharedPreferencesCompat.apply(editor);
	} 
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:edit
		socializeSessionFactory = AndroidMock.createMock(SocializeSessionFactory.class);
	} 
	 
	public void testPreferencePersistDelete() throws JSONException { 
 
		AndroidMock.expect(context.getSharedPreferences("SocializeSession", Context.MODE_PRIVATE)).andReturn(prefs);
		AndroidMock.expect(prefs.edit()).andReturn(editor);
		AndroidMock.expect(editor.clear()).andReturn(editor);
		AndroidMock.expect(editor.commit()).andReturn(true);
		 
		AndroidMock.replay(context);
		AndroidMock.replay(prefs);
		AndroidMock.replay(editor);
		 
		PreferenceSessionPersister persister = new PreferenceSessionPersister(userFactory, socializeSessionFactory);
		 
		persister.delete(context);
		 
		AndroidMock.verify(context);
		AndroidMock.verify(prefs);

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:edit, getString
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
			 
			// Migrate preference to database if it is set. 
			SharedPreferences settings = mCtx.getSharedPreferences("PasswordActivity", Context.MODE_PRIVATE); 
			String lastFile = settings.getString(LAST_FILENAME, "");
			String lastKey = settings.getString(LAST_KEYFILE,"");
						 
			if ( lastFile.length() > 0 ) {
				ContentValues vals = new ContentValues();
				vals.put(KEY_FILE_FILENAME, lastFile);
				vals.put(KEY_FILE_UPDATED, System.currentTimeMillis());
				 
				if ( lastKey.length() > 0 ) {
					vals.put(KEY_FILE_KEYFILE, lastKey);
				} 
				 
				db.insert(FILE_TABLE, null, vals);
				 
				// Clear old preferences 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getString
    Log.i(TAG, "onTerminated");
  } 
 
  public synchronized Twitter getTwitter() { // <5> 
    if (this.twitter == null) {
      String username = this.prefs.getString("username", null);
      String password = this.prefs.getString("password", null);
      String apiRoot = prefs.getString("apiRoot",
          "http://yamba.marakana.com/api"); 
      if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)
          && !TextUtils.isEmpty(apiRoot)) {
        this.twitter = new Twitter(username, password);
        this.twitter.setAPIRootUrl(apiRoot);
      } 
    } 
    return this.twitter;
  } 
 
  public synchronized void onSharedPreferenceChanged( 
      SharedPreferences sharedPreferences, String key) { // <6>
7
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Android SharedPreferences Questions & Answers:

Other Android SharedPreferences Examples

0:	SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
1:	Editor editor = pref.edit();
2:

Read More

9:	public class SessionManager {
10:		// Shared Preferences
11:		SharedPreferences pref;
12:		
13:		// Editor for Shared preferences

Read More

9:	public class SessionManager {
10:		// Shared Preferences
11:		SharedPreferences pref;
12:		
13:		// Editor for Shared preferences

Read More

6:	        // Deletes previously stored password policy.
7:	        super.onDisabled(context, intent);
8:	        SharedPreferences prefs = context.getSharedPreferences(APP_PREF, Activity.MODE_PRIVATE);
9:	        prefs.edit().clear().commit();
10:	    }

Read More

See Code Examples for Android SharedPreferences Methods: