下面列出了android.content.SharedPreferences.Editor#putLong ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 保存数据到文件
*
* @param key
* @param data
*/
public static void saveData(String key, Object data) {
String type = data.getClass().getSimpleName();
Editor editor = sharedPreferences.edit();
if ("Integer".equals(type)) {
editor.putInt(key, (Integer) data);
} else if ("Boolean".equals(type)) {
editor.putBoolean(key, (Boolean) data);
} else if ("String".equals(type)) {
editor.putString(key, (String) data);
} else if ("Float".equals(type)) {
editor.putFloat(key, (Float) data);
} else if ("Long".equals(type)) {
editor.putLong(key, (Long) data);
}
editor.apply();
}
/**
* 保存数据到文件
*
* @param context
* @param key
* @param data
*/
public static void saveData(Context context, String key, Object data) {
SharedPreferences sharedPreferences = context
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
if (data instanceof Integer) {
editor.putInt(key, (Integer) data);
} else if (data instanceof Boolean) {
editor.putBoolean(key, (Boolean) data);
} else if (data instanceof String) {
editor.putString(key, (String) data);
} else if (data instanceof Float) {
editor.putFloat(key, (Float) data);
} else if(data instanceof Double) {
editor.putFloat(key, Float.valueOf(data + ""));
}else if (data instanceof Long) {
editor.putLong(key, (Long) data);
}
editor.apply();
}
public static void put(Context context, String spName, String key, Object object) {
SharedPreferences sp = context.getSharedPreferences(spName,
Context.MODE_PRIVATE);
Editor editor = sp.edit();
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
editSubmit(editor);
}
private static void a(String str, Object obj) {
Editor edit = c.edit();
if (obj.getClass() == Boolean.class) {
edit.putBoolean(str, ((Boolean) obj).booleanValue());
}
if (obj.getClass() == String.class) {
edit.putString(str, (String) obj);
}
if (obj.getClass() == Integer.class) {
edit.putInt(str, ((Integer) obj).intValue());
}
if (obj.getClass() == Float.class) {
edit.putFloat(str, (float) ((Float) obj).intValue());
}
if (obj.getClass() == Long.class) {
edit.putLong(str, (long) ((Long) obj).intValue());
}
edit.commit();
}
/**
* Retrieve internal id of call handler as saved in databases It should be
* some negative < SipProfile.INVALID_ID number
*
* @param ctxt Application context
* @param packageName name of the call handler package
* @return the id of this call handler in databases
*/
public static Long getAccountIdForCallHandler(Context ctxt, String packageName) {
SharedPreferences prefs = ctxt.getSharedPreferences("handlerCache", Context.MODE_PRIVATE);
long accountId = SipProfile.INVALID_ID;
try {
accountId = prefs.getLong(VIRTUAL_ACC_PREFIX + packageName, SipProfile.INVALID_ID);
} catch (Exception e) {
Log.e(THIS_FILE, "Can't retrieve call handler cache id - reset");
}
if (accountId == SipProfile.INVALID_ID) {
// We never seen this one, add a new entry for account id
int maxAcc = prefs.getInt(VIRTUAL_ACC_MAX_ENTRIES, 0x0);
int currentEntry = maxAcc + 1;
accountId = SipProfile.INVALID_ID - (long) currentEntry;
Editor edt = prefs.edit();
edt.putLong(VIRTUAL_ACC_PREFIX + packageName, accountId);
edt.putInt(VIRTUAL_ACC_MAX_ENTRIES, currentEntry);
edt.commit();
}
return accountId;
}
public static void putLong(String key, long value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
LeisureReadApp.getAppContext());
Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
editor.commit();
}
/** save session time */
public void setSessionTime() {
SharedPreferences preferences2sessiontime = context.getSharedPreferences("mobclick_agent_state_" + packageName, preferenceMode);
Editor editor = preferences2sessiontime.edit();
long currenttime = System.currentTimeMillis();
editor.putLong("session_save_time", currenttime);
editor.commit();
}
/**
* 保存 Token 对象到 SharedPreferences。
*
* @param context 应用程序上下文环境
* @param token Token 对象
*/
public static void writeAccessToken(Context context, Oauth2AccessToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(KEY_UID, token.getUid());
editor.putString(KEY_ACCESS_TOKEN, token.getToken());
editor.putString(KEY_REFRESH_TOKEN, token.getRefreshToken());
editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
editor.commit();
}
public static void putLong(String key, long value)
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(HeroVideoApp.getInstance());
Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
editor.apply();
}
/**
* Saves the double as a long raw bits inside the preferences.
*
* @param key The name of the preference to modify.
* @param value The double value to be save in the preferences.
* @see Editor#putLong(String, long)
*/
public static void putDouble(final String key, final double value) {
final Editor editor = getPreferences().edit();
editor.putLong(key, Double.doubleToRawLongBits(value));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
editor.commit();
} else {
editor.apply();
}
}
/**
* 保存 Token 对象到 SharedPreferences。
*
* @param context 应用程序上下文环境
* @param token Token 对象
*/
public static void writeAccessToken(Context context, Oauth2AccessToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(KEY_UID, token.getUid());
editor.putString(KEY_ACCESS_TOKEN, token.getToken());
editor.putString(KEY_REFRESH_TOKEN, token.getRefreshToken());
editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
editor.commit();
}
/**
* Sets a long preference value.
*
* @param context the context
* @param keyId the key id
* @param value the value
*/
@SuppressLint("CommitPrefEdits")
public static void setLong(Context context, int keyId, long value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putLong(getKey(context, keyId), value);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
}
/**
* 保存 Token 对象到 SharedPreferences。
*
* @param context 应用程序上下文环境
* @param token Token 对象
*/
public static void writeAccessToken(Context context, Oauth2AccessToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(KEY_UID, token.getUid());
editor.putString(KEY_ACCESS_TOKEN, token.getToken());
editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
editor.commit();
}
/** save the time of post log */
public void setLastReportTime(long time) {
SharedPreferences preferences = context.getSharedPreferences("mobclick_agent_state_" + packageName, preferenceMode);
Editor edit = preferences.edit();
edit.putLong("last_report_time", time);
edit.commit();
}
public void f(Context context) {
SharedPreferences a = x.a(context);
if (a != null) {
String b = b(context);
Editor edit = a.edit();
edit.putString(c, b);
edit.putLong(a, System.currentTimeMillis());
edit.putLong(b, 0);
edit.putLong("a_start_time", System.currentTimeMillis());
edit.putLong("a_end_time", 0);
edit.commit();
bv.d("Restart session: " + b);
}
}
private void storeRegistrationId(Context context, String regId) {
Editor ed = app.getSettings().edit();
ed.putString("gcm_reg_id", regId);
ed.putLong("gcm_reg_last_version", app.getAppVersion());
ed.apply();
}
public void loadSharedPreferences(SharedPreferences preferences)
{
Editor editor = preferences.edit();
editor.putLong(PREF_EVENT_ID, this._id);
editor.putString(PREF_EVENT_NAME, this._name);
editor.putString(PREF_EVENT_PROFILE_START, Long.toString(this._fkProfileStart));
editor.putString(PREF_EVENT_PROFILE_END, Long.toString(this._fkProfileEnd));
editor.putBoolean(PREF_EVENT_ENABLED, this._status != ESTATUS_STOP);
editor.putString(PREF_EVENT_NOTIFICATION_SOUND_START, this._notificationSoundStart);
editor.putBoolean(PREF_EVENT_NOTIFICATION_VIBRATE_START, this._notificationVibrateStart);
editor.putBoolean(PREF_EVENT_NOTIFICATION_REPEAT_START, this._repeatNotificationStart);
editor.putString(PREF_EVENT_NOTIFICATION_REPEAT_INTERVAL_START, String.valueOf(this._repeatNotificationIntervalStart));
editor.putString(PREF_EVENT_NOTIFICATION_SOUND_END, this._notificationSoundEnd);
editor.putBoolean(PREF_EVENT_NOTIFICATION_VIBRATE_END, this._notificationVibrateEnd);
editor.putBoolean(PREF_EVENT_FORCE_RUN, this._forceRun);
//editor.putBoolean(PREF_EVENT_UNDONE_PROFILE, this._undoneProfile);
editor.putString(PREF_EVENT_PRIORITY_APP_SETTINGS, Integer.toString(this._priority));
editor.putString(PREF_EVENT_PRIORITY, Integer.toString(this._priority));
editor.putString(PREF_EVENT_DELAY_START, Integer.toString(this._delayStart));
editor.putString(PREF_EVENT_AT_END_DO, Integer.toString(this._atEndDo));
editor.putBoolean(PREF_EVENT_MANUAL_PROFILE_ACTIVATION, this._manualProfileActivation);
editor.putString(PREF_EVENT_START_WHEN_ACTIVATED_PROFILE, this._startWhenActivatedProfile);
editor.putString(PREF_EVENT_DELAY_END, Integer.toString(this._delayEnd));
editor.putBoolean(PREF_EVENT_NO_PAUSE_BY_MANUAL_ACTIVATION, this._noPauseByManualActivation);
this._eventPreferencesTime.loadSharedPreferences(preferences);
this._eventPreferencesBattery.loadSharedPreferences(preferences);
this._eventPreferencesCall.loadSharedPreferences(preferences);
this._eventPreferencesPeripherals.loadSharedPreferences(preferences);
this._eventPreferencesCalendar.loadSharedPreferences(preferences);
this._eventPreferencesWifi.loadSharedPreferences(preferences);
this._eventPreferencesScreen.loadSharedPreferences(preferences);
this._eventPreferencesBluetooth.loadSharedPreferences(preferences);
this._eventPreferencesSMS.loadSharedPreferences(preferences);
this._eventPreferencesNotification.loadSharedPreferences(preferences);
this._eventPreferencesApplication.loadSharedPreferences(preferences);
this._eventPreferencesLocation.loadSharedPreferences(preferences);
this._eventPreferencesOrientation.loadSharedPreferences(preferences);
this._eventPreferencesMobileCells.loadSharedPreferences(preferences);
this._eventPreferencesNFC.loadSharedPreferences(preferences);
this._eventPreferencesRadioSwitch.loadSharedPreferences(preferences);
this._eventPreferencesAlarmClock.loadSharedPreferences(preferences);
this._eventPreferencesDeviceBoot.loadSharedPreferences(preferences);
editor.apply();
}
/**
* Reads a single preference and sets it into the given editor.
*
* @param name the name of the preference to read
* @param typeId the type ID of the preference to read
* @param reader the reader to read from
* @param editor the editor to set the preference in
* @throws IOException if there are errors while reading
*/
private void readAndSetPreference(String name, byte typeId, DataInputStream reader, Editor editor)
throws IOException {
boolean save = true;
if (doNotBackup.contains(name)) {
save = false;
}
switch (typeId) {
case ContentTypeIds.BOOLEAN_TYPE_ID:
boolean booleanValue = reader.readBoolean();
if (save) {
editor.putBoolean(name, booleanValue);
}
return;
case ContentTypeIds.LONG_TYPE_ID:
long longValue = reader.readLong();
if (save) {
editor.putLong(name, longValue);
}
return;
case ContentTypeIds.FLOAT_TYPE_ID:
float floatValue = reader.readFloat();
if (save) {
editor.putFloat(name, floatValue);
}
return;
case ContentTypeIds.INT_TYPE_ID:
int intValue = reader.readInt();
if (save) {
editor.putInt(name, intValue);
}
return;
case ContentTypeIds.STRING_TYPE_ID:
String utfValue = reader.readUTF();
if (save) {
editor.putString(name, utfValue);
}
return;
}
}
protected static void a(String str, long j) {
Editor edit = a.edit();
edit.putLong(str, j);
edit.apply();
}
/**
* 保存long值
*
* @param context 上下文
* @param key 键
* @param value 值
*/
public static void putLong(Context context, String key, long value) {
SharedPreferences sp = getSp(context);
Editor editor = sp.edit();
editor.putLong(key, value);
editor.apply();
}