下面列出了android.content.SharedPreferences.Editor#commit ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 保存用户名密码
*
* @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;
}
/**
* 批量存储数据
* @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();
}
/**
* 批量移除喜好配置中的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;
}
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();
}
/**
* �����ť
*
* @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("���洢����");
}
}
/**
* 清空 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();
}
/**
* 清空数据
*/
public static void reset(final Context ctx) {
Editor edit = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
edit.clear();
edit.commit();
}
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();
}
public static void putValue(Context context, String key, boolean value) {
Editor sp = PreferenceManager.getDefaultSharedPreferences(context)
.edit();
sp.putBoolean(key, value);
sp.commit();
}
@TargetApi(9)
public boolean save(Editor editor) {
if (VERSION.SDK_INT < 9) {
return editor.commit();
}
editor.apply();
return true;
}
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();
}
/**
* 清空 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();
}
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();
}
public static boolean putInt(String key, int value)
{
Editor editor = edit();
editor.putInt(key, value);
return editor.commit();
}
public static void clearPreference(Context context,
final SharedPreferences p) {
final Editor editor = p.edit();
editor.clear();
editor.commit();
}
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();
}
public void setDataNum(String num) {
Editor editor = context.getSharedPreferences(SETTINGS, 4).edit();
editor.putString("dataNum", num);
editor.commit();
}
private void commit(final Editor ed) {
if (!ed.commit()) {
throw new IllegalStateException("Failed to commit new SharedPreferences value");
}
}
public static void putString(Context context, String key, String value) {
SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
Editor edit = sharedPreferences.edit();
edit.putString(key, value);
edit.commit();
}
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();
}