How to save and get integer value in general preferences in android?

I want to save and get the static integer value of Snow Density in the general settings and change when the user selects another value in a single selection.
My code is:

public static int mSnowDensity; AlertDialog.Builder mABuilder = new AlertDialog.Builder(AAA.this); final CharSequence mCharSequence[] = { "Low", "Medium", "High" }; mABuilder.setTitle("Set Density of Snow"); mABuilder.setSingleChoiceItems(mCharSequence, WallpaperServices.mDensitySnow, new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == 2) { mSnowDensity = 90; /*I Want to save mSnowDensity Value In Shared Preferences */ } else if (which == 1) { mSnowDensity = 60; } else { mSnowDensity = 30; } dialog.dismiss(); } }); 
+4
source share
6 answers

You can use general settings as follows

 //To save SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt("SNOW_DENSITY",mSnowDensity); editor.commit(); //To retrieve SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0); int snowDensity = settings.getInt("SNOW_DENSITY", 0); //0 is the default value 

getSharedPreferences () is a method of the Context class. If you are inside an Activity or Service (which expand the context), you can use it, as in this snippet. In addition, you must get the context using getApplicationContext (), and then call the getSharedPreferences () method.

Additional options can be found in the documentation at http://developer.android.com/guide/topics/data/data-storage.html#pref

+12
source

To save to SharedPreferences:

 private final String PREFS_NAME = "filename"; private final String KEY_DENSITY = "den"; Context ctx = getApplicationContext(); SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(KEY_DENSITY, mSnowDensity); editor.commit(); 

To get the value:

 Context ctx = getApplicationContext(); String strSavedValue = null; SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); strSavedValue = sharedPreferences.getInt("den", anyDefaultValue); 
+1
source

Save value to prefrence

 private void SavePreferences(String key, int value) { SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt(key, value); editor.commit(); } 

get value out of preference

  private void showPreferences(String key){ SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); int savedPref = sharedPreferences.getInt(key, 0); } 

You can use the key as a shared privilege name.

0
source

Preserving preferences is not a concern. But if you have many such custom options, you can use PreferenceActivity and override onSharedPreferenceChanged .

More details here http://developer.android.com/guide/topics/ui/settings.html

0
source

Initialization We need an editor to edit and save changes in the general settings. The following code can be used to get general settings.

  SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 :- for private mode Editor editor = pref.edit(); 

Data storage

 editor.putBoolean("key_name", true); editor.putString("key_name", "string value"); editor.putInt("key_name", "int value"); editor.putFloat("key_name", "float value"); editor.putLong("key_name", "long value"); editor.commit(); 

Data extraction

 pref.getString("key_name", null); // getting String pref.getInt("key_name", -1); // getting Integer pref.getFloat("key_name", null); // getting Float pref.getLong("key_name", null); // getting Long pref.getBoolean("key_name", null); // getting boolean 

Clear or delete data

 editor.remove("name"); // will delete key name editor.remove("email"); // will delete key email editor.commit(); // commit changes editor.clear(); editor.commit(); // commit changes 
0
source

UPDATE 2018:

You can just use the PowerPreference library

https://github.com/AliEsaAssadi/Android-Power-Preference

Getting an instance of general preference

To get the default instance, you just need to call

 PowerPreference.defult() 

Or if you want a specific settings file

 PowerPreference.name("YOUR_PREF_NAME") 

Data Recording:

 PowerPreference.defult().put("SNOW_DENSITY",value) 

Data retrieval

 PowerPreference.defult().getInt("SNOW_DENSITY") 
0
source

Source: https://habr.com/ru/post/1494013/


All Articles