下面列出了android.content.SharedPreferences.Editor#putInt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
* @author zhoufeng
* @createtime 2015-3-30 下午2:59:33
* @Decription 向SharedPreferences添加信息
*
* @param context 上下文
* @param SharedPreferName SharedPreferences的名称
* @param type 数据的类型
* @param key 数据的名称
* @param value 数据的值
*/
public static void saveSharedPreferInfo(Context context, String SharedPreferName, String type, String key,
Object value) {
SharedPreferences userPreferences;
userPreferences = context.getSharedPreferences(SharedPreferName, Context.MODE_PRIVATE);
Editor editor = userPreferences.edit();
if ("String".equals(type)) {
editor.putString(key, (String) value);
} else if ("Integer".equals(type)) {
editor.putInt(key, (Integer) value);
} else if ("Boolean".equals(type)) {
editor.putBoolean(key, (Boolean) value);
} else if ("Float".equals(type)) {
editor.putFloat(key, (Float) value);
} else if ("Long".equals(type)) {
editor.putLong(key, (Long) value);
}
editor.commit();
}
/**
* 保存用户名密码
*
* @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;
}
/**有些时候MarketApplication.getInstance()是为null的,比如升级数据库的时候*/
public static boolean setIntValue(Context context,String key, int value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefs.edit();
editor.putInt(key, value);
return editor.commit();
}
public static void setInteger(String key, int value) {
SharedPreferences sp = mContext.getSharedPreferences(
SHARED_PREFERANCE_NAME, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putInt(key, value);
editor.apply();
}
public void saveProfileList(Context context) {
SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedprefs.edit();
editor.putStringSet("vpnlist", profiles.keySet());
// For reasing I do not understand at all
// Android saves my prefs file only one time
// if I remove the debug code below :(
int counter = sharedprefs.getInt("counter", 0);
editor.putInt("counter", counter + 1);
editor.apply();
}
/**
* Put integer data to shared preferences in private mode.
* @param context - The context of activity which is requesting to put data.
* @param key - Used to identify the value.
* @param value - The actual value to be saved.
*/
public static void putInt(Context context, String key, int value) {
SharedPreferences mainPref =
context.getSharedPreferences(context.getResources()
.getString(R.string.shared_pref_package),
Context.MODE_PRIVATE
);
Editor editor = mainPref.edit();
editor.putInt(key, value);
editor.commit();
}
SpUtils putIntCommit(String key, int value) {
if (mPref != null) {
Editor editor = mPref.edit();
editor.putInt(key, value);
editor.commit();
return this;
} else throw new RuntimeException("First Initialize context");
}
/**
* Put integer data to shared preferences in private mode.
* @param context - The context of activity which is requesting to put data.
* @param key - Used to identify the value.
* @param value - The actual value to be saved.
*/
public static void putInt(Context context, String key, int value) {
SharedPreferences mainPref =
context.getSharedPreferences(context.getResources()
.getString(R.string.shared_pref_package),
Context.MODE_PRIVATE
);
Editor editor = mainPref.edit();
editor.putInt(key, value);
editor.commit();
}
/**
* Put integer data to shared preferences in private mode.
* @param context - The context of activity which is requesting to put data.
* @param key - Used to identify the value.
* @param value - The actual value to be saved.
*/
public static void putInt(Context context, String key, int value) {
SharedPreferences mainPref =
context.getSharedPreferences(context.getResources()
.getString(R.string.shared_pref_package),
Context.MODE_PRIVATE
);
Editor editor = mainPref.edit();
editor.putInt(key, value);
editor.commit();
}
public static void a(Context context, String str, int i) {
if (a(context)) {
b(context);
Editor edit = a.edit();
edit.putInt(str, i);
edit.apply();
return;
}
z.d();
}
/**
* Save current audio mode in order to be able to restore it once done
*/
@SuppressWarnings("deprecation")
private synchronized void saveAudioState() {
if( prefs.getBoolean("isSavedAudioState", false) ) {
//If we have already set, do not set it again !!!
return;
}
ContentResolver ctntResolver = service.getContentResolver();
Editor ed = prefs.edit();
// ed.putInt("savedVibrateRing", audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER));
// ed.putInt("savedVibradeNotif", audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION));
// ed.putInt("savedRingerMode", audioManager.getRingerMode());
ed.putInt("savedWifiPolicy" , Compatibility.getWifiSleepPolicy(ctntResolver));
int inCallStream = Compatibility.getInCallStream(userWantBluetooth);
ed.putInt("savedVolume", audioManager.getStreamVolume(inCallStream));
int targetMode = getAudioTargetMode();
if(service.getPrefs().useRoutingApi()) {
ed.putInt("savedRoute", audioManager.getRouting(targetMode));
}else {
ed.putBoolean("savedSpeakerPhone", audioManager.isSpeakerphoneOn());
}
ed.putInt("savedMode", audioManager.getMode());
ed.putBoolean("isSavedAudioState", true);
ed.commit();
}
/**
* @param key The name of the preference to modify.
* @param value The new value for the preference.
* @see Editor#putStringSet(String, Set)
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void putStringSet(final String key, final Set<String> value) {
final Editor editor = getPreferences().edit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
editor.putStringSet(key, value);
} else {
// Workaround for pre-HC's lack of StringSets
int stringSetLength = 0;
if (mPrefs.contains(key + LENGTH)) {
// First read what the value was
stringSetLength = mPrefs.getInt(key + LENGTH, -1);
}
editor.putInt(key + LENGTH, value.size());
int i = 0;
for (String aValue : value) {
editor.putString(key + "[" + i + "]", aValue);
i++;
}
for (; i < stringSetLength; i++) {
// Remove any remaining values
editor.remove(key + "[" + i + "]");
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
editor.commit();
} else {
editor.apply();
}
}
public static void setInteger(Context context, String key, int value) {
SharedPreferences sp = context.getSharedPreferences(
SHARED_PREFERANCE_NAME, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putInt(key, value);
editor.commit();
}
public void saveInt(String key, int value) {
Editor edit = this.settings.edit();
edit.putInt(key, value);
edit.commit();
}
public void setIntPreference(String key, int value) {
Editor ed = preference.edit();
ed.putInt(key, value);
ed.apply();
}
public void setLastSleepTimerValue(final int value) {
final Editor editor = mPreferences.edit();
editor.putInt(LAST_SLEEP_TIMER_VALUE, value);
editor.apply();
}
public void setContinuePayDateTime(String datetime) {
Editor editor = context.getSharedPreferences(SETTINGS, 4).edit();
editor.putString("continue_pay_datetime", datetime);
editor.putInt("expire_days", -101);
editor.commit();
}
public void saveAppMoveStatus() {
SharedPreferences mSharedPreferences = thisActivity.getSharedPreferences("app_order", Context.MODE_PRIVATE);
Editor editor = mSharedPreferences.edit();
editor.clear();
for (int i = 0; i < mAppInfos.size(); i++) {
editor.putInt(mAppInfos.get(i).mAppName, i);
}
editor.apply();
}
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();
}
public void setExpireDays(int expireDays) {
Editor editor = context.getSharedPreferences(SETTINGS, 4).edit();
editor.putInt("expire_days", expireDays);
editor.commit();
}