Shared Preferences is allow you to save and retrive data in the form of key, value pair.You can use sharedpreferences to save any primitive data,booleans,floats,ints,longs and string.
[code lang=”js”]getSharedPreferences()- Return sharedPreferences instance poiting to the file that contains the values of preferences. [/code]
[code lang=”js”]SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); [/code]
You can save something in the sharedpreferences by using SharedPreferences.Editor class. You will call the edit method of SharedPreference instance and will recieve it in an editor object.
[code lang=”js”]
public static final String USER_DETAILS_PREFS = "user_details_prefs";
private static final String NAME = "name";
public static String getName(Context context) {
SharedPreferences sp = context.getSharedPreferences(
USER_DETAILS_PREFS, Activity.MODE_PRIVATE);
return sp.getString(NAME, "");
}
public static void setUserDetail(Context context, String name) {
SharedPreferences sf = context.getSharedPreferences(
USER_DETAILS_PREFS, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sf.edit();
editor.putString(NAME, name);
editor.commit();
}
getPreferences()- Use this from an Activity if you need to use only one shared preference file for the activity.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
[/code]
[code lang=”js”]
SharedPreferences sf = context.getSharedPreferences(
My_PREFS, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sf.edit();
editor.remove(“key_name1”);
editor.remove(“key_name2”);
editor.commit();
[/code]
[code lang=”js”]
editor.clear();
editor.commit();
[/code]
Warning: _() expects exactly 1 parameter, 2 given in /home2/mobilemeri7/public_html/wp-content/themes/mobilemerit/comments.php on line 28