当前位置: 首页>>代码示例>>Java>>正文


Java Context.getSharedPreferences方法代码示例

本文整理汇总了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();
}
 
开发者ID:interritus1996,项目名称:memento-app,代码行数:21,代码来源:StorageHelper.java

示例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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:31,代码来源:UserSessionManager.java

示例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();
    }
}
 
开发者ID:huashengzzz,项目名称:SmartChart,代码行数:24,代码来源:SPUtils.java

示例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;
}
 
开发者ID:weixianshishen,项目名称:LookWeather,代码行数:9,代码来源:SPUtil.java

示例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;
}
 
开发者ID:codekongs,项目名称:ImageClassify,代码行数:16,代码来源:SaveDataUtil.java

示例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);
}
 
开发者ID:Zyj163,项目名称:yyox,代码行数:13,代码来源:DataHelper.java

示例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;
}
 
开发者ID:snowwolf10285,项目名称:PicShow-zhaipin,代码行数:36,代码来源:DataHelper.java

示例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);
}
 
开发者ID:codeccc,项目名称:baselibrary-master,代码行数:14,代码来源:SPUtils.java

示例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);
}
 
开发者ID:orgzly,项目名称:orgzly-android,代码行数:8,代码来源:SyncStatus.java

示例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();
}
 
开发者ID:pedromassango,项目名称:Programmers,代码行数:8,代码来源:PrefsUtil.java

示例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;
	}
}
 
开发者ID:smartbeng,项目名称:PaoMovie,代码行数:20,代码来源:HelperSP.java

示例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();
}
 
开发者ID:sdrausty,项目名称:buildAPKsSamples,代码行数:9,代码来源:Policy.java

示例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();
}
 
开发者ID:haihaio,项目名称:AmenEye,代码行数:9,代码来源:SpUtil.java

示例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();
}
 
开发者ID:yufeilong92,项目名称:update-master,代码行数:10,代码来源:UpdateUtil.java

示例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);
}
 
开发者ID:shenhuanet,项目名称:AndroidOpen,代码行数:5,代码来源:PreferenceUtils.java


注:本文中的android.content.Context.getSharedPreferences方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。