android.content.Context.getSharedPreferences()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(311)

本文整理了Java中android.content.Context.getSharedPreferences()方法的一些代码示例,展示了Context.getSharedPreferences()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.getSharedPreferences()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:getSharedPreferences

Context.getSharedPreferences介绍

暂无

代码示例

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 返回存在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);
}

代码示例来源:origin: Tencent/tinker

/**
 * you can set Tinker disable in runtime at some times!
 *
 * @param context
 */
public static void setTinkerDisableWithSharedPreferences(Context context) {
  SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
  String keyName = ShareConstants.TINKER_ENABLE_CONFIG_PREFIX + ShareConstants.TINKER_VERSION;
  sp.edit().putBoolean(keyName, false).commit();
}

代码示例来源:origin: stackoverflow.com

private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";

public synchronized static String id(Context context) {
  if (uniqueID == null) {
    SharedPreferences sharedPrefs = context.getSharedPreferences(
        PREF_UNIQUE_ID, Context.MODE_PRIVATE);
    uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
    if (uniqueID == null) {
      uniqueID = UUID.randomUUID().toString();
      Editor editor = sharedPrefs.edit();
      editor.putString(PREF_UNIQUE_ID, uniqueID);
      editor.commit();
    }
  }
  return uniqueID;
}

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldStoreValues() throws Exception {
 editor.commit();
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 assertTrue(anotherSharedPreferences.getBoolean("boolean", false));
 assertThat(anotherSharedPreferences.getFloat("float", 666f)).isEqualTo(1.1f);
 assertThat(anotherSharedPreferences.getInt("int", 666)).isEqualTo(2);
 assertThat(anotherSharedPreferences.getLong("long", 666L)).isEqualTo(3L);
 assertThat(anotherSharedPreferences.getString("string", "wacka wa")).isEqualTo("foobar");
 assertThat(anotherSharedPreferences.getStringSet("stringSet", null)).isEqualTo(stringSet);
}

代码示例来源:origin: guardianproject/haven

public PreferenceManager(Context context) {
  this.context = context;
  this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, AppCompatActivity.MODE_PRIVATE);
  this.prefsEditor = appSharedPrefs.edit();
}

代码示例来源:origin: TommyLemon/APIJSON

/**
 * @param paramList
 * @return
 */
public String getToken(String tag) {
  return context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE).getString(KEY_TOKEN + tag, "");
}
/**

代码示例来源:origin: stackoverflow.com

public class AppPreferences {
   public static final String KEY_PREFS_SMS_BODY = "sms_body";
   private static final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); //  Name of the file -.xml
   private SharedPreferences _sharedPrefs;
   private Editor _prefsEditor;

   public AppPreferences(Context context) {
     this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
     this._prefsEditor = _sharedPrefs.edit();
   }

   public String getSmsBody() {
     return _sharedPrefs.getString(KEY_PREFS_SMS_BODY, "");
   }

   public void saveSmsBody(String text) {
     _prefsEditor.putString(KEY_PREFS_SMS_BODY, text);
     _prefsEditor.commit();
   }
}

代码示例来源:origin: GitLqr/LQRWeChat

private SPUtils(Context context) {
  this.context = context;
  sp = this.context.getSharedPreferences(SP_NAME, Context.MODE_APPEND);
  editor = sp.edit();
}

代码示例来源:origin: TommyLemon/APIJSON

/**
 * @param paramList
 * @must demo_***改为服务器设定值
 * @return
 */
public String getToken(String tag) {
  return context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE).getString(KEY_TOKEN + tag, "");
}
/**

代码示例来源:origin: czy1121/update

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();
}

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 存储重要信息到sharedPreferences;
 *
 * @param key
 * @param value
 */
public static void setStringSF(Context context, String key, String value) {
  if (mSharedPreferences == null) {
    mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  }
  mSharedPreferences.edit().putString(key, value).apply();
}

代码示例来源:origin: TommyLemon/APIJSON

/**
 * @return
 */
public String getCookie() {
  return context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE).getString(KEY_COOKIE, "");
}
/**

代码示例来源:origin: robolectric/robolectric

@Test
public void commit_shouldClearEditsThatNeedRemoveAndEditsThatNeedCommit() throws Exception {
 editor.commit();
 editor.remove("string").commit();
 assertThat(sharedPreferences.getString("string", "no value for key")).isEqualTo("no value for key");
 SharedPreferences anotherSharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
 anotherSharedPreferences.edit().putString("string", "value for key").commit();
 editor.commit();
 assertThat(sharedPreferences.getString("string", "no value for key")).isEqualTo("value for key");
}

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 清除某个内容
 */
public static void removeSF(Context context, String key) {
  if (mSharedPreferences == null) {
    mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  }
  mSharedPreferences.edit().remove(key).apply();
}

代码示例来源:origin: TommyLemon/APIJSON

/**
 * @return
 */
public String getCookie() {
  return context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE).getString(KEY_COOKIE, "");
}
/**

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 存储重要信息到sharedPreferences;
 *
 * @param key
 * @param value
 */
public static void setIntergerSF(Context context, String key, int value) {
  if (mSharedPreferences == null) {
    mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  }
  mSharedPreferences.edit().putInt(key, value).apply();
}

代码示例来源:origin: TommyLemon/APIJSON

/**
 * @param tag
 * @must demo_***改为服务器设定值
 * @return
 */
public String getToken(String tag) {
  return context.getSharedPreferences(KEY_TOKEN, Context.MODE_PRIVATE).getString(KEY_TOKEN + tag, "");
}
/**

代码示例来源:origin: jiangqqlmj/FastDev4Android

private SharedPreferencesHelper(Context context) {
  sp = context.getSharedPreferences(SHARED_PATH, Context.MODE_PRIVATE);
  editor = sp.edit();
}

代码示例来源:origin: TommyLemon/APIJSON

/**
 * @return
 */
public String getCookie() {
  return context.getSharedPreferences(KEY_COOKIE, Context.MODE_PRIVATE).getString(KEY_COOKIE, "");
}
/**

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 清除Shareprefrence
 */
public static void clearShareprefrence(Context context) {
  if (mSharedPreferences == null) {
    mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
  }
  mSharedPreferences.edit().clear().apply();
}

相关文章

Context类方法