下面列出了android.content.SharedPreferences.Editor#putBoolean ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 存放object
*
* @param context
* @param fileName
* @param key
* @param object
* @return
*/
public static boolean put(Context context, String fileName, String key, Object object) {
SharedPreferences sp = context.getSharedPreferences(fileName, 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).intValue());
} else if (object instanceof Boolean) {
editor.putBoolean(key, ((Boolean) object).booleanValue());
} else if (object instanceof Float) {
editor.putFloat(key, ((Float) object).floatValue());
} else if (object instanceof Long) {
editor.putLong(key, ((Long) object).longValue());
} else if (object instanceof Set) {
editor.putStringSet(key, (Set<String>) object);
} else {
editor.putStringSet(key, (Set<String>) object);
}
return editor.commit();
}
public static void setBoolean(Context context, String key, boolean value) {
SharedPreferences sp = context.getSharedPreferences(
SHARED_PREFERANCE_NAME, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putBoolean(key, value);
editor.commit();
}
void loadSharedPreferences(SharedPreferences preferences)
{
Editor editor = preferences.edit();
editor.putBoolean(PREF_EVENT_SMS_ENABLED, _enabled);
//editor.putString(PREF_EVENT_SMS_EVENT, String.valueOf(this._smsEvent));
editor.putString(PREF_EVENT_SMS_CONTACTS, this._contacts);
editor.putString(PREF_EVENT_SMS_CONTACT_GROUPS, this._contactGroups);
editor.putString(PREF_EVENT_SMS_CONTACT_LIST_TYPE, String.valueOf(this._contactListType));
editor.putBoolean(PREF_EVENT_SMS_PERMANENT_RUN, this._permanentRun);
editor.putString(PREF_EVENT_SMS_DURATION, String.valueOf(this._duration));
editor.apply();
}
/**
* Put boolean 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 putBoolean(Context context, String key, boolean value) {
SharedPreferences mainPref =
context.getSharedPreferences(context.getResources()
.getString(R.string.shared_pref_package),
Context.MODE_PRIVATE
);
Editor editor = mainPref.edit();
editor.putBoolean(key, value);
editor.commit();
}
public void setGameShow(boolean isShow) {
Editor editor = context.getSharedPreferences(SETTINGS, 4).edit();
editor.putBoolean("isGameShow", isShow);
if (LetvUtils.isSpecialChannel()) {
editor.putBoolean("isGameShow", false);
}
editor.commit();
}
public static void putBoolean(String key, boolean value)
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(HeroVideoApp.getInstance());
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Entered OnReceive in NetworkConnectedReceiver");
//Skipping non admin users
if (ActivityManager.getCurrentUser() != 0) {
Log.i(TAG, "The user serial number is " + ActivityManager.getCurrentUser());
} else {
Log.i(TAG, "The user is admin");
SharedPreferences prefs = context.getSharedPreferences(EMM_PREFERENCES, Context.MODE_PRIVATE);
Boolean isAgentCalled = prefs.getBoolean(EMM_AGENT_HAS_BEEN_CALLED, false);
if (!isAgentCalled) {
//Check network connected
if (isConnected(context)) {
Intent agentLauncherIntent = new Intent();
agentLauncherIntent.setComponent(new ComponentName(Constants.AGENT_APP_PACKAGE_NAME,
Constants.AGENT_APP_PACKAGE_NAME + Constants.AGENT_APP_LAUNCH_ACTIVITY));
agentLauncherIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
agentLauncherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(agentLauncherIntent);
Editor editor = prefs.edit();
editor.putBoolean(EMM_AGENT_HAS_BEEN_CALLED, true);
editor.apply();
Log.i(TAG, "EMM agent has been called");
}
}
}
}
/**
* @param key The name of the preference to modify.
* @param value The new value for the preference.
* @see Editor#putBoolean(String, boolean)
*/
public static void putBoolean(final String key, final boolean value) {
final Editor editor = getPreferences().edit();
editor.putBoolean(key, value);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
editor.commit();
} else {
editor.apply();
}
}
/** 设置Key对应的boolean值 */
public static boolean setBoolValue(Context context,String key, boolean value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefs.edit();
editor.putBoolean(key, value);
return editor.commit();
}
public static void setBoolean(Context context, String key, boolean value) {
SharedPreferences sp = context.getSharedPreferences(
SHARED_PREFERANCE_NAME, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putBoolean(key, value);
editor.commit();
}
public void fixPersistentSettings() {
final String badDebugName = "DEBUG_MODE\"";
final String badNotificationName = "NOTFICATION_ICON";
final String badLockScreenName = "LOCK_SCREEN\"";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Map<String, ?> prefNames = prefs.getAll();
// Don't do anything if the bad preferences have already been fixed.
if (!prefNames.containsKey(badDebugName) &&
!prefNames.containsKey(badNotificationName) &&
!prefNames.containsKey(badLockScreenName)) {
return;
}
Editor editor = prefs.edit();
if (prefNames.containsKey(badDebugName)) {
editor.putString(AppSettings.DEBUG_MODE, prefs.getString(badDebugName, null));
editor.remove(badDebugName);
}
if (prefNames.containsKey(badNotificationName)){
editor.putBoolean(AppSettings.NOTIFICATION_ICON, prefs.getBoolean(badNotificationName, true));
editor.remove(badNotificationName);
}
if (prefNames.containsKey(badLockScreenName)) {
editor.putString(AppSettings.LOCK_SCREEN, prefs.getString(badLockScreenName, null));
editor.remove(badLockScreenName);
}
editor.apply();
}
public void setColoredAppShortcuts(final boolean value) {
final Editor editor = mPreferences.edit();
editor.putBoolean(COLORED_APP_SHORTCUTS, value);
editor.apply();
}
@Override
void setPreferenceValue(Editor editor, @StringRes int valueKey, Boolean value) {
String key = context.getString(valueKey);
editor.putBoolean(key, value);
}
public void setChannelShow(boolean isShow) {
Editor editor = context.getSharedPreferences(SETTINGS, 4).edit();
editor.putBoolean("isChannelShow", isShow);
editor.commit();
}
public static void a(Context context, String str, boolean z) {
Editor edit = a(context).edit();
edit.putBoolean(str, z);
edit.apply();
}
private void setPreferenceValue(Context context, int type, boolean value) {
Editor editor = context.getSharedPreferences("gesture", 0).edit();
editor.putBoolean(type == 0 ? "isFirstPush" : "isFirstDownload", false);
editor.commit();
}
public static void setWidgetExistsPreference(Context context, int[] appWidgetIds) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
for (int appWidgetId : appWidgetIds) {
String widgetExists = WIDGET_EXISTS_PREFIX.concat(Integer.toString(appWidgetId));
editor.putBoolean(widgetExists, true);
}
editor.commit();
}
public static void setWidgetExistsPreference(Context context, int[] appWidgetIds) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
for (int appWidgetId : appWidgetIds) {
String widgetExists = WIDGET_EXISTS_PREFIX.concat(Integer.toString(appWidgetId));
editor.putBoolean(widgetExists, true);
}
editor.apply();
}
public static void setWidgetExistsPreference(Context context, int[] appWidgetIds) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
for (int appWidgetId : appWidgetIds) {
String widgetExists = WIDGET_EXISTS_PREFIX.concat(Integer.toString(appWidgetId));
editor.putBoolean(widgetExists, true);
}
editor.commit();
}
/**
* 存储Boolean值到配置文件
*
* @param preName 配置文件名
* @param key 键值
* @param value 需要存储的boolean值
*/
public static Boolean putBoolean(String preName, Context context, String key, Boolean value) {
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
Editor editor = pre.edit();
editor.putBoolean(key, value);
return editor.commit();
}