Application shared preferences allows you to save and retrieve key, value pair data. Before getting into tutorial, I am giving basic information needed to work with shared preferences.
Initialization
Application shared preferences can be fetched using getSharedPreferences() method.You also need an editor to edit and save the changes in shared preferences. The following code can be used to get application shared preferences.
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Storing Data
You can save data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. Call editor.commit() in order to save changes to shared preferences.
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
editor.commit(); // commit changes
Retrieving Data
Data can be retrived from saved preferences by calling getString() (For string) method. Remember this method should be called on Shared Preferences not on Editor.
//returns stored preference value
//If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Clearing / Deleting Data
If you want to delete from shared preferences you can call remove(“key_name”) to delete that particular value. If you want to delete all the data, call clear()
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
Following will clear all the data from shared preferences
editor.clear();
editor.commit(); // commit changes
Code for SharedPreference
public class SessionManager {
// http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
SharedPreferences pref;
Editor editor;
Context context;
int PRIVATE_MODE = 0;
String prefName = "eatPref";
String key_NAME = "username";
String key_PASS = "password";
String IS_LOGIN = "IsLoggedIn";
//constructor
public SessionManager(Context context) {
this.context = context;
pref = context.getSharedPreferences(prefName, PRIVATE_MODE);
editor = pref.edit();
}
public void login(String username, String password) {
editor.putBoolean(IS_LOGIN, true);
editor.putString(key_NAME, username);
editor.putString(key_PASS, password);
editor.commit();
}
public HashMap<String, String> getDetails() {
HashMap<String, String> user = null;
user.put(key_NAME, pref.getString(key_NAME, null));
user.put(key_PASS, pref.getString(key_PASS, null));
return user;
}
// Get Login State
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
Initialization
Application shared preferences can be fetched using getSharedPreferences() method.You also need an editor to edit and save the changes in shared preferences. The following code can be used to get application shared preferences.
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Storing Data
You can save data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. Call editor.commit() in order to save changes to shared preferences.
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
editor.commit(); // commit changes
Retrieving Data
Data can be retrived from saved preferences by calling getString() (For string) method. Remember this method should be called on Shared Preferences not on Editor.
//returns stored preference value
//If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Clearing / Deleting Data
If you want to delete from shared preferences you can call remove(“key_name”) to delete that particular value. If you want to delete all the data, call clear()
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
Following will clear all the data from shared preferences
editor.clear();
editor.commit(); // commit changes
Code for SharedPreference
public class SessionManager {
// http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
SharedPreferences pref;
Editor editor;
Context context;
int PRIVATE_MODE = 0;
String prefName = "eatPref";
String key_NAME = "username";
String key_PASS = "password";
String IS_LOGIN = "IsLoggedIn";
//constructor
public SessionManager(Context context) {
this.context = context;
pref = context.getSharedPreferences(prefName, PRIVATE_MODE);
editor = pref.edit();
}
public void login(String username, String password) {
editor.putBoolean(IS_LOGIN, true);
editor.putString(key_NAME, username);
editor.putString(key_PASS, password);
editor.commit();
}
public HashMap<String, String> getDetails() {
HashMap<String, String> user = null;
user.put(key_NAME, pref.getString(key_NAME, null));
user.put(key_PASS, pref.getString(key_PASS, null));
return user;
}
// Get Login State
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
LoginActivity:
public class Login extends Activity {
LinearLayout Signin;
EditText username;
EditText password;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.login_main);
// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
// R.layout.header);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
password.setTypeface(Typeface.DEFAULT);
Signin = (LinearLayout) findViewById(R.id.signin_ll);
Signin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SessionManager sm = new SessionManager(getApplicationContext());
sm.login(username.getText().toString().trim().toLowerCase(),
password.getText().toString().trim().toLowerCase());
}
}
});
}
No comments:
Post a Comment