android.content.SharedPreferences.getString()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(180)

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

SharedPreferences.getString介绍

[英]Retrieve a String value from the preferences.
[中]从首选项中检索字符串值。

代码示例

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

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
 String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
 int idName = prefs.getInt("idName", 0); //0 is the default value.
}

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

final SharedPreferences prefs = new ObscuredSharedPreferences( 
  this, this.getSharedPreferences(MY_PREFS_FILE_NAME, Context.MODE_PRIVATE) );

// eg.    
prefs.edit().putString("foo","bar").commit();
prefs.getString("foo", null);

代码示例来源: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: 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: stackoverflow.com

public String[] loadArray(String arrayName, Context mContext) {  
  SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
  int size = prefs.getInt(arrayName + "_size", 0);  
  String array[] = new String[size];  
  for(int i=0;i<size;i++)  
    array[i] = prefs.getString(arrayName + "_" + i, null);  
  return array;  
}

代码示例来源:origin: ankidroid/Anki-Android

preferences.edit().remove("noSpaceLeft").apply();
} else if (preferences.getString("lastVersion", "").equals("")) {
  preferences.edit().putString("lastVersion", VersionUtils.getPkgVersionName()).apply();
  onFinishedStartup();
} else if (skip < 2 && !preferences.getString("lastVersion", "").equals(VersionUtils.getPkgVersionName())) {
  } else {
    try {
      previous = preferences.getInt("lastUpgradeVersion", current);
      Timber.i("Previous AnkiDroid version: %s", previous);
    } catch (ClassCastException e) {
      String s = preferences.getString("lastUpgradeVersion", "");
  preferences.edit().putInt("lastUpgradeVersion", current).apply();
      preferences.edit().putString("lastVersion", VersionUtils.getPkgVersionName()).apply();
      String ver = getResources().getString(R.string.updated_version, VersionUtils.getPkgVersionName());
      UIUtils.showSnackbar(this, ver, true, -1, null, findViewById(R.id.root_layout), null);

代码示例来源:origin: mikepenz/AboutLibraries

List<Library> libraries;
PackageInfo pi = Util.getPackageInfo(ctx);
SharedPreferences sharedPreferences = ctx.getSharedPreferences("aboutLibraries", Context.MODE_PRIVATE);
int lastCacheVersion = sharedPreferences.getInt("versionCode", -1);
boolean isCacheUpToDate = pi != null && lastCacheVersion == pi.versionCode;
    String[] autoDetectedLibraries = sharedPreferences.getString("autoDetectedLibraries", "").split(DELIMITER);
  sharedPreferences.edit()
      .putInt("versionCode", pi.versionCode)
      .putString("autoDetectedLibraries", autoDetectedLibrariesPref.toString())
      .apply();

代码示例来源: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: f2prateek/rx-preferences

@Test public void set() {
 rxPreferences.getBoolean("foo1").set(false);
 assertThat(preferences.getBoolean("foo1", true)).isFalse();
 rxPreferences.getEnum("foo2", PAPER, Roshambo.class).set(ROCK);
 assertThat(preferences.getString("foo2", null)).isEqualTo("ROCK");
 rxPreferences.getFloat("foo3").set(1f);
 assertThat(preferences.getFloat("foo3", 0f)).isEqualTo(1f);
 rxPreferences.getInteger("foo4").set(1);
 assertThat(preferences.getInt("foo4", 0)).isEqualTo(1);
 rxPreferences.getLong("foo5").set(1L);
 assertThat(preferences.getLong("foo5", 0L)).isEqualTo(1L);
 rxPreferences.getString("foo6").set("bar");
 assertThat(preferences.getString("foo6", null)).isEqualTo("bar");
 rxPreferences.getStringSet("foo7").set(singleton("bar"));
 assertThat(preferences.getStringSet("foo7", null)).isEqualTo(singleton("bar"));
 rxPreferences.getObject("foo8", new Point(2, 3), pointConverter).set(new Point(1, 2));
 assertThat(preferences.getString("foo8", null)).isEqualTo("1,2");
}

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

hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname"));
ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1"));
editor.putString(getKey() + "_host",hostBox.getText().toString());
editor.putString(getKey() + "_ip",ipBox.getText().toString());
editor.commit();

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

public static void setDefaults(String key, String value, Context context) {
  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
  SharedPreferences.Editor editor = prefs.edit();
  editor.putString(key, value);
  editor.commit();
}

public static String getDefaults(String key, Context context) {
  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
  return preferences.getString(key, null);
}

代码示例来源: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: 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 String[] loadArray(String arrayName, Context mContext) {  
  SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
  int size = prefs.getInt(arrayName + "_size", 0);  
  String array[] = new String[size];  
  for(int i=0;i<size;i++)  
    array[i] = prefs.getString(arrayName + "_" + i, null);  
  return array;  
}

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

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) 
{
 //mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
 int selectionStart = prefs.getInt("selection-start", -1);
 int selectionEnd = prefs.getInt("selection-end", -1);
 /*if (selectionStart != -1 && selectionEnd != -1)
 {
   mSaved.setSelection(selectionStart, selectionEnd);
 }*/
}

代码示例来源:origin: ac-pm/Inspeckage

@Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    SharedPreferences.Editor edit = mPrefs.edit();
    if (isChecked) {
      edit.putBoolean(Config.SP_SWITCH_AUTH, true);
    } else {
      edit.putBoolean(Config.SP_SWITCH_AUTH, false);
    }
    TextView txtLogin = (TextView) view.findViewById(R.id.txtLogin);
    TextView txtPass = (TextView) view.findViewById(R.id.txtPass);
    edit.putString(Config.SP_USER_PASS, txtLogin.getText()+":"+txtPass.getText());
    edit.apply();
    stopService();
    String host = null;
    int port = mPrefs.getInt(Config.SP_SERVER_PORT, 8008);
    if(!mPrefs.getString(Config.SP_SERVER_HOST, "All interfaces").equals("All interfaces")){
      host = mPrefs.getString(Config.SP_SERVER_HOST, "All interfaces");
    }
    startService(host,port);
  }
});

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

@Test
public void putString_shouldRemovePairIfValueIsNull() throws Exception {
 editor.putString("deleteMe", "foo");
 editor.putString("deleteMe", null);
 editor.commit();
 assertThat(sharedPreferences.getString("deleteMe", null)).isNull();
}

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

//------get sharedPreferences

SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

//-------get a value from them

pref.getString("NAME", "Android");

//--------modify the value

pref.edit().putString("NAME", "Simone").commit();

//--------reset preferences

pref.edit().clear().commit();

代码示例来源: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: TommyLemon/APIJSON

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

相关文章