Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. public class SessionManager{
  2.  
  3. // Shared Preferences
  4. SharedPreferences pref;
  5.  
  6. // Editor for Shared preferences
  7. Editor editor;
  8.  
  9. // Context
  10. Context _context;
  11.  
  12. // Shared pref mode
  13. int PRIVATE_MODE = 0;
  14.  
  15. // Sharedpref file name
  16. private static final String PREF_NAME = "AndroidHivePref";
  17.  
  18. // All Shared Preferences Keys
  19. private static final String IS_LOGIN = "IsLoggedIn";
  20.  
  21. // User name (make variable public to access from outside)
  22. public static final String KEY_userid = "userid";
  23. //public static final String KEY_NAME = "name";
  24.  
  25. // Email address (make variable public to access from outside)
  26. public static final String KEY_EMAIL = "email";
  27.  
  28. // Constructor
  29. public SessionManager(Context context){
  30. this._context = context;
  31. pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
  32. editor = pref.edit();
  33. }
  34.  
  35. /**
  36. * Create login session
  37. * */
  38. public void createLoginSession(String userid, String email){
  39. // Storing login value as TRUE
  40. editor.putBoolean(IS_LOGIN, true);
  41.  
  42. // Storing name in pref
  43. editor.putString(KEY_userid, userid);
  44. //editor.putString(KEY_NAME, name);
  45.  
  46. // Storing email in pref
  47. editor.putString(KEY_EMAIL, email);
  48.  
  49. // commit changes
  50. editor.commit();
  51. }
  52.  
  53. /**
  54. * Check login method will check user login status
  55. * If false it will redirect user to login page
  56. * Else won't do anything
  57. * */
  58. public void checkLogin(){
  59. // Check login status
  60. if(!this.isLoggedIn()){
  61. // user is not logged in redirect him to Login Activity
  62. Intent i = new Intent(_context, Login.class);
  63. // Closing all the Activities
  64. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  65.  
  66. // Add new Flag to start new Activity
  67. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  68.  
  69. // Staring Login Activity
  70. _context.startActivity(i);
  71. }
  72.  
  73. }
  74.  
  75.  
  76.  
  77. /**
  78. * Get stored session data
  79. * */
  80. public HashMap<String, String> getUserDetails(){
  81. HashMap<String, String> user = new HashMap<String, String>();
  82. // user name
  83. user.put(KEY_userid, pref.getString(KEY_userid, null));
  84.  
  85. //user.put(KEY_NAME, pref.getString(KEY_NAME, null));
  86.  
  87. // user email id
  88. user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
  89.  
  90. // return user
  91. return user;
  92. }
  93.  
  94. /**
  95. * Clear session details
  96. * */
  97. public void logoutUser(){
  98. // Clearing all data from Shared Preferences
  99. editor.clear();
  100. editor.commit();
  101.  
  102. //Toast.makeText(SessionManager.this, "function call...: " , Toast.LENGTH_LONG).show();
  103.  
  104. // After logout redirect user to Loing Activity
  105. Intent i = new Intent(_context, Login.class);
  106. // Closing all the Activities
  107. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  108.  
  109. // Add new Flag to start new Activity
  110. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  111.  
  112. // Staring Login Activity
  113. _context.startActivity(i);
  114.  
  115. //Intent intent = new Intent(getApplicationContext(), Second_activity.class);
  116. //startActivity(intent);
  117.  
  118. }
  119.  
  120. /**
  121. * Quick check for login
  122. * **/
  123. // Get Login State
  124. public boolean isLoggedIn(){
  125. return pref.getBoolean(IS_LOGIN, false);
  126. }
  127.  
  128.  
  129.  
  130. }
  131.  
  132. In this class i removed extra code just write relevant code where userid save in session after login success,
  133. public class Login extends Activity {
  134.  
  135. private EditText editTextUserName;
  136. private EditText editTextPassword;
  137. SessionManager session;
  138. String userid,;
  139.  
  140. String username;
  141. String password,regid;
  142.  
  143.  
  144. @Override
  145. protected void onCreate(Bundle savedInstanceState) {
  146. super.onCreate(savedInstanceState);
  147. setContentView(R.layout.activity_registeratin);
  148.  
  149.  
  150. session = new SessionManager(getApplicationContext());
  151.  
  152. editTextUserName = (EditText) findViewById(R.id.nameID);
  153. editTextPassword = (EditText) findViewById(R.id.et_pass_ID);
  154.  
  155.  
  156. }
  157.  
  158. respObject.getJSONObject("response");
  159. userid = responses.getString("memberID");
  160.  
  161.  
  162. String active = respObject.getString("status");
  163. if(active.equalsIgnoreCase("200")){
  164.  
  165.  
  166. session.createLoginSession(userid, "anroidhive@gmail.com");
  167.  
  168.  
  169. Intent intent=new Intent(Login.this,Welcome.class);
  170.  
  171. startActivity(intent);
  172. finish();
  173.  
  174. }
  175.  
  176. }
  177.  
  178. Here i want to retrieve userid in this class that i saved before in login class, here i am writing only relevant code .
  179. public class Welcome extends Activity {
  180.  
  181. SessionManager session;
  182.  
  183.  
  184. @Override
  185. protected void onCreate(Bundle savedInstanceState) {
  186. super.onCreate(savedInstanceState);
  187. setContentView(R.layout.activity_welcome);
  188.  
  189. HashMap<String, String> user = session.getUserDetails();
  190. String userid = user.get(SessionManager.KEY_userid);
  191.  
  192.  
  193. Toast.makeText(this, userid+"", Toast.LENGTH_LONG).show();
  194. }
  195. }
  196.  
  197. public class Welcome extends Activity {
  198.  
  199. SessionManager session;
  200.  
  201.  
  202. @Override
  203. protected void onCreate(Bundle savedInstanceState) {
  204. super.onCreate(savedInstanceState);
  205. setContentView(R.layout.activity_welcome);
  206.  
  207. session = new SessionManager(getApplicationContext());//<-- this is what you missed
  208. HashMap<String, String> user = session.getUserDetails();
  209. String userid = user.get(SessionManager.KEY_userid);
  210.  
  211.  
  212. Toast.makeText(this, userid+"", Toast.LENGTH_LONG).show();
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement