Java Code Examples for android.content.SharedPreferences.Editor#commit()

The following examples show how to use android.content.SharedPreferences.Editor#commit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SharedPreferencesUtils.java    From fitness_Android with Apache License 2.0 6 votes vote down vote up
/**
 * 保存用户名密码
 *
 * @param context
 * @param user
 * @return
 */
public static boolean saveUserInfo(Context context, User user) {
    try {
        //1.通过Context对象创建一个SharedPreference对象
        //name:sharedpreference文件的名称    mode:文件的操作模式
        SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        //2.通过sharedPreferences对象获取一个Editor对象
        Editor editor = sharedPreferences.edit();
        //3.往Editor中添加数据
        editor.putInt("userId", user.getUserId());
        editor.putString("username", user.getUsername());
        editor.putString("password", user.getPassword());
        editor.putString("sex", user.getSex());
        editor.putString("height", user.getHeight() + "");
        editor.putString("weight", user.getWeight() + "");
        //4.提交Editor对象
        editor.commit();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 2
Source File: PreferVisitor.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 批量存储数据
 * @param spFileName
 * @param keys
 * @param values
 */
public boolean batchSaveValues(String spFileName, String[] keys, Object... values) {
    if (Util.isEmpty(spFileName) || keys == null || values == null) {
        return false;
    }
    int keysLen = keys.length;
    int valuesLen = values.length;
    int canOptLen = Math.min(keysLen, valuesLen);
    if (canOptLen == 0) {
        return false;
    }
    Editor editor = getSpByName(spFileName).edit();
    for(int i = 0; i < canOptLen ; i++) {
        String key = keys[i];
        if (Util.isEmpty(key)) {
            continue;
        }
        editorPutValues(editor,key,values[i]);
    }
    return editor.commit();
}
 
Example 3
Source File: PreferVisitor.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/**
 * 批量移除喜好配置中的keys
 * @param spFileName
 * @param spKeys
 */
public boolean batchRemoveKeys(String spFileName, String... spKeys) {
    if (spKeys != null && spKeys.length > 0) {
        Editor spFileEditor = getEditor(spFileName);
        for (String toRemoveKey : spKeys) {
            spFileEditor.remove(toRemoveKey);
        }
        return spFileEditor.commit();
    }
    return false;
}
 
Example 4
Source File: PreferenceUtils.java    From LeisureRead with Apache License 2.0 5 votes vote down vote up
public static void putStringProcess(String key, String value) {

    SharedPreferences sharedPreferences = LeisureReadApp.getAppContext()
        .getSharedPreferences("preference_mu", Context.MODE_MULTI_PROCESS);
    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
  }
 
Example 5
Source File: MainActivity.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * �����ť
 * 
 * @param view
 */
public void gxlogin(View view) {
	// System.out.println("�����ť�ˣ��洢��������");
	String qq = etQQ.getText().toString().trim();
	String pwd = etPwd.getText().toString().trim();
	if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
		Toast.makeText(this, "QQ�Ż������벻��Ϊ�գ�����", 0).show();
		return;
	}

	// �û��Ƿ�ѡ����ס����
	boolean isChecked = cbRem.isChecked();
	if (isChecked) {
		// �洢����

		// 2. ��ȡ�༭��Editor
		Editor edit = mSp.edit();
		// 3. �ñ༭���洢����
		edit.putString("qq", qq);
		edit.putString("mm", pwd);
		// 4. ��Ҫ����ס�ύ����
		edit.commit();

		Toast.makeText(this, "���ݴ洢�ɹ���", 0).show();
	} else {
		System.out.println("���洢����");
	}

}
 
Example 6
Source File: AccessTokenKeeper.java    From ChinaShare with MIT License 5 votes vote down vote up
/**
 * 清空 SharedPreferences 中 Token信息。
 * 
 * @param context 应用程序上下文环境
 */
public static void clear(Context context) {
    if (null == context) {
        return;
    }
    
    SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
    Editor editor = pref.edit();
    editor.clear();
    editor.commit();
}
 
Example 7
Source File: PreferenceUtils.java    From LeisureRead with Apache License 2.0 5 votes vote down vote up
/**
 * 清空数据
 */
public static void reset(final Context ctx) {

  Editor edit = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
  edit.clear();
  edit.commit();
}
 
Example 8
Source File: SettingsShortcutActivity.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
static public void writeDefaultShortcuts(Context context)
{
    final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor = sp.edit();
    for( DefineShortcut sd : TBL_SHORTCUT )
    {
        String key = KEY_SHORTCUT + sd.key;
        if ( !sp.contains(key) ){
            editor.putString(key, Integer.toString(sd.function) );
        }
    }
    editor.commit();
}
 
Example 9
Source File: SharedPrefsUtil.java    From zidoorecorder with Apache License 2.0 5 votes vote down vote up
public static void putValue(Context context, String key, boolean value) {

		Editor sp = PreferenceManager.getDefaultSharedPreferences(context)
				.edit();
		sp.putBoolean(key, value);
		sp.commit();
	}
 
Example 10
Source File: PreferenceStoreImpl.java    From letv with Apache License 2.0 5 votes vote down vote up
@TargetApi(9)
public boolean save(Editor editor) {
    if (VERSION.SDK_INT < 9) {
        return editor.commit();
    }
    editor.apply();
    return true;
}
 
Example 11
Source File: PreferenceHelper.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public static void setDisplayLimitPreference(Context context, int value) {
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor = sharedPrefs.edit();

    editor.putString(context.getText(R.string.pref_display_limit).toString(), Integer.toString(value));

    editor.commit();
}
 
Example 12
Source File: AccessTokenKeeper.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
/**
 * 清空 SharedPreferences 中 Token信息。
 * 
 * @param context 应用程序上下文环境
 */
public static void clear(Context context) {
    if (null == context) {
        return;
    }
    
    SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
    Editor editor = pref.edit();
    editor.clear();
    editor.commit();
}
 
Example 13
Source File: SettingsActivity.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
public void colorChanged(int fg, int bg) {
    final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
    Editor editor = sp.edit();
    editor.putInt( KEY_UNDERLINE_COLOR, fg );
    editor.commit();
}
 
Example 14
Source File: SharedUtil.java    From FileManager with Apache License 2.0 4 votes vote down vote up
public static void putString(Context context, String key, String value) {
    SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
    Editor edit = sharedPreferences.edit();
    edit.putString(key, value);
    edit.commit();
}
 
Example 15
Source File: AedictApp.java    From aedict with GNU General Public License v3.0 4 votes vote down vote up
private void commit(final Editor ed) {
	if (!ed.commit()) {
		throw new IllegalStateException("Failed to commit new SharedPreferences value");
	}
}
 
Example 16
Source File: PreferencesManager.java    From letv with Apache License 2.0 4 votes vote down vote up
public void setDataNum(String num) {
    Editor editor = context.getSharedPreferences(SETTINGS, 4).edit();
    editor.putString("dataNum", num);
    editor.commit();
}
 
Example 17
Source File: SPUtil.java    From FastLib with Apache License 2.0 4 votes vote down vote up
public static boolean clearAll(Context context, String fileName) {
    SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    Editor editor = sp.edit();
    editor.clear();
    return editor.commit();
}
 
Example 18
Source File: PreferenceUtils.java    From WeGit with Apache License 2.0 4 votes vote down vote up
public static void clearPreference(Context context,
		final SharedPreferences p) {
	final Editor editor = p.edit();
	editor.clear();
	editor.commit();
}
 
Example 19
Source File: SdkPreference.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
public static boolean putInt(String key, int value)
{
	Editor editor = edit();
	editor.putInt(key, value);
	return editor.commit();
}
 
Example 20
Source File: SPUtil.java    From FastLib with Apache License 2.0 4 votes vote down vote up
public static boolean remove(Context context, String fileName, String key) {
    SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    Editor editor = sp.edit();
    editor.remove(key);
    return editor.commit();
}