Wednesday, 10 December 2014

Share Preference

Share Preference class (SessionStore Class)




public class SessionStore {


public static final String CONSINOR="consinor";

public static final String USER_ID = "userId";
public static final String USER_NAME = "userName";
public static final String PWD = "pwd";


public static final String CUSTOMER_ID = "customerId";
public static final String IPADD = "ipaddress";


public static boolean save(Context context,String name,String userName ,String pwd,String userId,String customerId,Boolean ischecked,String IpAdress) {
Editor editor =context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
editor.putBoolean("isChecked",ischecked );
editor.putString(USER_ID, userId);
editor.putString(USER_NAME, userName);
editor.putString(PWD, pwd);


editor.putString(CUSTOMER_ID, customerId);
editor.putString(IPADD, IpAdress);

return editor.commit();
}

public static HashMap<String, String> getUserDetails(Context context,String name){
SharedPreferences  pref = context.getSharedPreferences(name, Context.MODE_PRIVATE);
HashMap<String, String> user = new HashMap<String, String>();
// user
user.put("isChecked", ""+pref.getBoolean("isChecked", false));

// user login
user.put(USER_ID, pref.getString(USER_ID, null));

// user pwd
user.put(CUSTOMER_ID, pref.getString(CUSTOMER_ID, null));
user.put(IPADD, pref.getString(IPADD, null));
user.put(USER_NAME, pref.getString(USER_NAME, null));
user.put(PWD, pref.getString(PWD, null));



// return user
return user;
}

public static void clear(Context context, String name ) {
Editor editor = context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
}

}


Main Java Class



//Save share preference
 SessionStore.save(LoginActivity.this,consinorid, semail, spass,consinor);

//Get share preference 

HashMap<String, String> user = new HashMap<String, String>();
user = SessionStore.getUserDetails(getApplicationContext(), "Name of preference");
String name = user.get(SessionStore.USER_NAME);

//Clear Share Preference

SessionStore.clear(getApplicationContext(),"Name of preference");








No comments:

Post a Comment