下面列出了android.content.SharedPreferences.Editor#apply ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onDestroy() {
// 清理定时任务
mInjectorService.unregister(this.provider);
this.provider = null;
stopForeground(true);
LogUtil.w(TAG, "FloatWin onDestroy");
writeFileData(fileName, "destroy recording:" + new Date());
div = 0;
//
wm.removeView(view);
//InjectorService.getInstance().stopInjection();
SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("state", "stop");
editor.apply();
// 取消注册广播
super.onDestroy();
}
public static void setString(String key, String value) {
SharedPreferences sp = mContext.getSharedPreferences(
SHARED_PREFERANCE_NAME, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString(key, value);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void write(Context context, String fileName, String k,
String v) {
SharedPreferences preference = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = preference.edit();
editor.putString(k, v);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void remove(Context context, String fileName, String k) {
SharedPreferences preference = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = preference.edit();
editor.remove(k);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void write(Context context, String fileName, String k, int v) {
SharedPreferences preference = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = preference.edit();
editor.putInt(k, v);
editor.apply();
}
/**
* Clear client credentials.
* @param context - Application context.
*/
public static void clearClientCredentials(Context context) {
SharedPreferences mainPref = context.getSharedPreferences(Constants.PACKAGE_NAME, Context.MODE_PRIVATE);
Editor editor = mainPref.edit();
editor.putString(Constants.CLIENT_ID, null);
editor.putString(Constants.CLIENT_SECRET, null);
editor.apply();
}
void loadSharedPreferences(SharedPreferences preferences)
{
Editor editor = preferences.edit();
editor.putBoolean(PREF_EVENT_NFC_ENABLED, _enabled);
editor.putString(PREF_EVENT_NFC_NFC_TAGS, _nfcTags);
editor.putBoolean(PREF_EVENT_NFC_PERMANENT_RUN, this._permanentRun);
editor.putString(PREF_EVENT_NFC_DURATION, String.valueOf(this._duration));
editor.apply();
}
protected static void a(String str, long j) {
Editor edit = a.edit();
edit.putLong(str, j);
edit.apply();
}
public void setSettings() {
// apiKey = getMetaDataValue("ANDROIDPN_API_KEY");
// Log.i(LOGTAG, "apiKey=" + apiKey);
// // if (apiKey == null) {
// // Log.e(LOGTAG, "Please set the androidpn api key in the manifest file.");
// // throw new RuntimeException();
// // }
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
apiKey = mySharedPreferences.getString("prefApikey", "1234567890").trim();
xmppHost = mySharedPreferences.getString("prefXmpphost", "192.168.0.1").trim();
xmppPort = mySharedPreferences.getString("prefXmppport", "5222").trim();
email = mySharedPreferences.getString("prefEmail", "").trim();
pass = mySharedPreferences.getString("prefPass", "").trim();
user = mySharedPreferences.getString("prefUser", "").trim();
name = mySharedPreferences.getString("prefName", "").trim();
boolean prefNtfy = mySharedPreferences.getBoolean("prefNtfy",true);
boolean prefSound = mySharedPreferences.getBoolean("prefSound",true);
boolean prefVibrate = mySharedPreferences.getBoolean("prefVibrate",true);
boolean prefToast = mySharedPreferences.getBoolean("prefToast",false);
Log.i(LOGTAG, "apiKey=" + apiKey);
Log.i(LOGTAG, "xmppHost=" + xmppHost);
Log.i(LOGTAG, "xmppPort=" + xmppPort);
Log.i(LOGTAG, "user=" + user);
Log.i(LOGTAG, "name=" + name);
Log.i(LOGTAG, "email=" + email);
sharedPrefs = context.getSharedPreferences(
Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putString(Constants.API_KEY, apiKey);
editor.putString(Constants.VERSION, version);
editor.putString(Constants.XMPP_HOST, xmppHost);
editor.putString(Constants.XMPP_USERNAME, user);
editor.putString(Constants.XMPP_PASSWORD, pass);
editor.putString(Constants.XMPP_EMAIL, email);
editor.putString(Constants.NAME, name);
try {
editor.remove(Constants.SETTINGS_NOTIFICATION_ENABLED);
editor.remove(Constants.SETTINGS_SOUND_ENABLED);
editor.remove(Constants.SETTINGS_VIBRATE_ENABLED);
editor.remove(Constants.SETTINGS_TOAST_ENABLED);
} catch (Exception e) {
Log.d(LOGTAG, "Settings not removed");
}
editor.putBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED, prefNtfy);
editor.putBoolean(Constants.SETTINGS_SOUND_ENABLED, prefSound);
editor.putBoolean(Constants.SETTINGS_VIBRATE_ENABLED, prefVibrate);
editor.putBoolean(Constants.SETTINGS_TOAST_ENABLED, prefToast);
editor.putInt(Constants.XMPP_PORT, Integer.parseInt(xmppPort.trim()));
editor.putString(Constants.CALLBACK_ACTIVITY_PACKAGE_NAME,
callbackActivityPackageName);
editor.putString(Constants.CALLBACK_ACTIVITY_CLASS_NAME,
callbackActivityClassName);
editor.apply();
// Log.i(LOGTAG, "sharedPrefs=" + sharedPrefs.toString());
}
public static void a(Context context, String str, int i) {
Editor edit = a(context).edit();
edit.putInt(str, i);
edit.apply();
}
public void setClassicNotification(final boolean value) {
final Editor editor = mPreferences.edit();
editor.putBoolean(CLASSIC_NOTIFICATION, value);
editor.apply();
}
public void setSongSortOrder(String sortOrder) {
Editor edit = this.mPreferences.edit();
edit.putString(SONG_SORT_ORDER, sortOrder);
edit.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();
}
public static void a(Context context, String str, boolean z) {
Editor edit = a(context).edit();
edit.putBoolean(str, z);
edit.apply();
}
public void setLastPage(final int value) {
final Editor editor = mPreferences.edit();
editor.putInt(LAST_PAGE, value);
editor.apply();
}
@Override
public void applyPreferenceChanges(Editor editor) {
// Apply asynchronously
editor.apply();
}
public void remove(String key) {
Editor editor = mPref.edit();
editor.remove(key);
editor.apply();
}
public void setFloatPreference(String key, float value) {
Editor ed = preference.edit();
ed.putFloat(key, value);
ed.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();
}
/**
* 写boolean配置到本地
* @param file 存储的文件名
* @param key key
* @param value 结果
*/
public static void writeBoolean(String file, String key, boolean value) {
Editor editor = context.getSharedPreferences(file, Context.MODE_PRIVATE).edit();
editor.putBoolean(key, value);
editor.apply();
}