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

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

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

SharedPreferences.edit介绍

[英]Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

Note that you must call Editor#commit to have any changes you perform in the Editor actually show up in the SharedPreferences.
[中]为这些首选项创建一个新的编辑器,通过它可以修改首选项中的数据,并自动将这些更改提交回SharedReferences对象。
请注意,您必须调用编辑器#提交,以使您在编辑器中执行的任何更改实际显示在SharedReferences中。

代码示例

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

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

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

final String PREFS_NAME = "MyPrefsFile";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

if (settings.getBoolean("my_first_time", true)) {
  //the app is being launched for first time, do something        
  Log.d("Comments", "First time");

       // first time task

  // record the fact that the app has been started at least once
  settings.edit().putBoolean("my_first_time", false).commit(); 
}

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

public class MyActivity extends Activity {

  SharedPreferences prefs = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Perhaps set content view here

    prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
  }

  @Override
  protected void onResume() {
    super.onResume();

    if (prefs.getBoolean("firstrun", true)) {
      // Do first run stuff here then set 'firstrun' as false
      // using the following line to edit/commit prefs
      prefs.edit().putBoolean("firstrun", false).commit();
    }
  }
}

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

//--Init
int myvar = 12;

//--SAVE Data
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);  
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("var1", myvar);
editor.commit();

//--READ data       
myvar = preferences.getInt("var1", 0);

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

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstRun = p.getBoolean(PREFERENCE_FIRST_RUN, true);
p.edit().putBoolean(PREFERENCE_FIRST_RUN, false).commit();

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

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Harneet");
editor.apply();

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

private SharedPreferences mPreferences;
....
boolean firstTime = mPreferences.getBoolean("firstTime", true);
if (firstTime) { 
  SharedPreferences.Editor editor = mPreferences.edit();
  editor.putBoolean("firstTime", false);
  editor.commit();
  showMiddleActivity();
}

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

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: stackoverflow.com

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
 boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
 if(!previouslyStarted) {
   SharedPreferences.Editor edit = prefs.edit();
   edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
   edit.commit();
   showHelp();
 }

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

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

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

private static final String MAP = "map";    
private static final Type MAP_TYPE = new TypeToken<Map<MyObjA, MyObjB>>() {}.getType();

private static SharedPreferences prefs = MyApplication.getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

private static Map<MyObjA, MyObjB> myMap;

public static void saveMap (Map<MyObjA, MyObjB> map) {
  SharedPreferences.Editor editor = prefs.edit();

  editor.putString(MAP, new Gson().toJson(map));
  editor.commit();
  myMap = map;
}

public static Map<MyObjA, MyObjB> loadMap() {
  if (myMap == null) {
    myMap = new Gson().fromJson(prefs.getString(MAP, null), MAP_TYPE);
  }
  return myMap;
}

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

SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun)
{
  // Code to run once
  SharedPreferences.Editor editor = wmbPreference.edit();
  editor.putBoolean("FIRSTRUN", false);
  editor.commit();
}

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

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

代码示例来源:origin: TeamNewPipe/NewPipe

private static MediaFormat getDefaultFormat(Context context, @StringRes int defaultFormatKey, @StringRes int defaultFormatValueKey) {
  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
  String defaultFormat = context.getString(defaultFormatValueKey);
  String defaultFormatString = preferences.getString(context.getString(defaultFormatKey), defaultFormat);
  MediaFormat defaultMediaFormat = getMediaFormatFromKey(context, defaultFormatString);
  if (defaultMediaFormat == null) {
    preferences.edit().putString(context.getString(defaultFormatKey), defaultFormat).apply();
    defaultMediaFormat = getMediaFormatFromKey(context, defaultFormat);
  }
  return defaultMediaFormat;
}

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

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
  // run your one time code
  SharedPreferences.Editor editor = prefs.edit();
  editor.putBoolean("firstTime", true);
  editor.commit();
}

相关文章