本文整理汇总了Java中android.content.Context.getSharedPreferences方法的典型用法代码示例。如果您正苦于以下问题:Java Context.getSharedPreferences方法的具体用法?Java Context.getSharedPreferences怎么用?Java Context.getSharedPreferences使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.content.Context
的用法示例。
在下文中一共展示了Context.getSharedPreferences方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setPersonName
import android.content.Context; //导入方法依赖的package包/类
public static void setPersonName(String personIdToAdd, String personName, String personGroupId, Context context) {
SharedPreferences personIdNameMap =
context.getSharedPreferences(personGroupId + "PersonIdNameMap", Context.MODE_PRIVATE);
SharedPreferences.Editor personIdNameMapEditor = personIdNameMap.edit();
personIdNameMapEditor.putString(personIdToAdd, personName);
personIdNameMapEditor.commit();
Set<String> personIds = getAllPersonIds(personGroupId, context);
Set<String> newPersonIds = new HashSet<>();
for (String personId: personIds) {
newPersonIds.add(personId);
}
newPersonIds.add(personIdToAdd);
SharedPreferences personIdSet =
context.getSharedPreferences(personGroupId + "PersonIdSet", Context.MODE_PRIVATE);
SharedPreferences.Editor personIdSetEditor = personIdSet.edit();
personIdSetEditor.putStringSet("PersonIdSet", newPersonIds);
personIdSetEditor.commit();
}
示例2: getCurrentUser
import android.content.Context; //导入方法依赖的package包/类
/**
This static method can be called to get the logged in user.
It reads from the shared preferences and builds a SmartUser object and returns it.
If no user is logged in it returns null
*/
public static SmartUser getCurrentUser(Context context){
SmartUser smartUser = null;
SharedPreferences preferences = context.getSharedPreferences(Constants.USER_PREFS, Context.MODE_PRIVATE);
Gson gson = new Gson();
String sessionUser = preferences.getString(Constants.USER_SESSION, Constants.DEFAULT_SESSION_VALUE);
String user_type = preferences.getString(Constants.USER_TYPE, Constants.CUSTOMUSERFLAG);
if(!sessionUser.equals(Constants.DEFAULT_SESSION_VALUE)){
try {
switch (user_type) {
case Constants.FACEBOOKFLAG:
smartUser = gson.fromJson(sessionUser, SmartFacebookUser.class);
break;
case Constants.GOOGLEFLAG:
smartUser = gson.fromJson(sessionUser, SmartGoogleUser.class);
break;
default:
smartUser = gson.fromJson(sessionUser, SmartUser.class);
break;
}
}catch (Exception e){
Log.e("GSON", e.getMessage());
}
}
return smartUser;
}
示例3: put
import android.content.Context; //导入方法依赖的package包/类
public static void put(Context context, String key, Object object, boolean sync) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.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());
}
if (sync) {
editor.apply();
} else {
editor.commit();
}
}
示例4: getInstance
import android.content.Context; //导入方法依赖的package包/类
public static SPUtil getInstance(Context c) {
if (spu == null) {
spu = new SPUtil();
spu.sp = c.getSharedPreferences("rfb", 0);
spu.edit = spu.sp.edit();
}
return spu;
}
示例5: getValueFromSharedPreferences
import android.content.Context; //导入方法依赖的package包/类
/**
* 从指定的文件中读取指定key的value
* @param context
* @param filename
* @param key
* @return
*/
public static String getValueFromSharedPreferences(Context context, String filename, String key){
String value = null;
SharedPreferences sharedPreferences = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
if (sharedPreferences != null){
value = sharedPreferences.getString(key, null);
}
return value;
}
示例6: getStringSF
import android.content.Context; //导入方法依赖的package包/类
/**
* 返回存在sharedPreferences的信息
*
* @param key
* @return
*/
public static String getStringSF(Context context, String key) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
return mSharedPreferences.getString(key, null);
}
示例7: getDeviceData
import android.content.Context; //导入方法依赖的package包/类
/**
* 将对象从shareprerence中取出来
*
* @param key
* @param <T>
* @return
*/
public static <T> T getDeviceData(Context context, String key) {
if (mSharedPreferences == null) {
mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
T device = null;
String productBase64 = mSharedPreferences.getString(key, null);
if (productBase64 == null) {
return null;
}
// 读取字节
byte[] base64 = Base64.decode(productBase64.getBytes(), Base64.DEFAULT);
// 封装到字节流
ByteArrayInputStream bais = new ByteArrayInputStream(base64);
try {
// 再次封装
ObjectInputStream bis = new ObjectInputStream(bais);
// 读取对象
device = (T) bis.readObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return device;
}
示例8: remove
import android.content.Context; //导入方法依赖的package包/类
/**
* 移除某个key值已经对应的值
*
* @param context
* @param key
*/
public static void remove(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}
示例9: loadFromPreferences
import android.content.Context; //导入方法依赖的package包/类
public void loadFromPreferences(Context context) {
SharedPreferences pref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
this.type = Type.valueOf(pref.getString(EXTRA_TYPE, Type.NOT_RUNNING.toString()));
this.message = pref.getString(EXTRA_MESSAGE, null);
this.currentBook = pref.getInt(EXTRA_CURRENT_BOOK, 0);
this.totalBooks = pref.getInt(EXTRA_TOTAL_BOOKS, 0);
}
示例10: saveToPreferences
import android.content.Context; //导入方法依赖的package包/类
public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(preferenceName, preferenceValue);
editor.apply();
}
示例11: getFromSP
import android.content.Context; //导入方法依赖的package包/类
/**
* 读出字段
* @param ctx
* @param title
* @param key
* @return String 如果未保存则返回null
*/
public static String getFromSP(Context ctx , String title,String key){
SharedPreferences se = ctx.getSharedPreferences(title, Context.MODE_PRIVATE);
if(se==null){
return null;
}
String value = se.getString(key, "");
if(value.equals("")){
return null;
}else{
return value;
}
}
示例12: onDisabled
import android.content.Context; //导入方法依赖的package包/类
@Override
public void onDisabled(Context context, Intent intent) {
// Called when the app is about to be deactivated as a device administrator.
// Deletes previously stored password policy.
super.onDisabled(context, intent);
SharedPreferences prefs = context.getSharedPreferences(APP_PREF, Activity.MODE_PRIVATE);
prefs.edit().clear().commit();
}
示例13: putBoolean
import android.content.Context; //导入方法依赖的package包/类
public static void putBoolean(Context context, String strKey,
Boolean strData) {
SharedPreferences activityPreferences = context.getSharedPreferences(
spFileName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = activityPreferences.edit();
editor.putBoolean(strKey, strData);
editor.commit();
}
示例14: clean
import android.content.Context; //导入方法依赖的package包/类
public static void clean(Context context) {
SharedPreferences sp = context.getSharedPreferences(PREFS, 0);
File file = new File(context.getExternalCacheDir(), sp.getString(KEY_UPDATE, "") + ".apk");
UpdateUtil.log("apk ==> " + file.toString());
if (file.exists()) {
file.delete();
}
sp.edit().clear().apply();
}
示例15: readInt
import android.content.Context; //导入方法依赖的package包/类
public static int readInt(Context context, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
return sharedPreferences.getInt(key, 0);
}