There are three mode available for store data in Preference
MODE_WORLD_READABLEMODE_WORLD_WRITEABLE
MODE_PRIVATE
Store,Read,Remove,Clear Data from Shared Preference
SharedPreferences prefrance = getApplicationContext().getSharedPreferences(
"MyPref"
, MODE_PRIVATE);
Editor edit= prefrance.edit();
/*********Data as KEY/VALUE pair *******************/
edit.putBoolean("key_name1", true);// Saving boolean - true/false
edit.putInt("key_name2", "int value");// Saving integer
edit.putFloat("key_name3", "float value");
edit.putLong("key_name4", "long value");
edit.putString("key_name5", "string value");
// Save the changes in SharedPreferences
edit.commit(); // commit changes
/**************** Get Preferences data *******************/
// If value for key not exist then return second param value - In this case null
prefrance.getBoolean("key_name1", null);
prefrance.getInt("key_name2", null);
prefrance.getFloat("key_name3", null);
prefrance.getLong("key_name4", null);
prefrance.getString("key_name5", null);
/********* Deleting Key value from Preferences **********/
edit.remove("key_name3"); // will delete key key_name3
edit.remove("key_name4"); // will delete key key_name4
// Save the changes in Preferences
edit.commit(); // commit changes
/********** Clear all data from Preferences *************/
edit.clear();
edit.commit(); // commit changes
No comments:
Post a Comment