-
-
Save dzwillpower/5963901 to your computer and use it in GitHub Desktop.
#SharedPreferences SharedPreferences 设置
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tediscript.android; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
public class Settings { | |
public static String PREFS_NAME = "com.tediscript.android.settings"; | |
public static void putString(Context ctx, String key, String value) { | |
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putString(key, value); | |
editor.commit(); | |
} | |
public static String getString(Context ctx, String key, String defaultValue) { | |
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0); | |
return settings.getString(key, defaultValue); | |
} | |
public static void putInt(Context ctx, String key, int value) { | |
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putInt(key, value); | |
editor.commit(); | |
} | |
public static int getInt(Context ctx, String key, int defaultValue) { | |
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0); | |
return settings.getInt(key, defaultValue); | |
} | |
public static void putBoolean(Context ctx, String key, boolean value) { | |
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putBoolean(key, value); | |
editor.commit(); | |
} | |
public static boolean getBoolean(Context ctx, String key, | |
boolean defaultValue) { | |
SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME, 0); | |
return settings.getBoolean(key, defaultValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment