erwinaji77n

Untitled

Dec 20th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package com.example.emfauzanashari.Model.Login;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6.  
  7. import com.example.emfauzanashari.nyoba.MainActivity;
  8.  
  9. import java.util.HashMap;
  10.  
  11. public class UserSession {
  12. SharedPreferences pref;
  13. SharedPreferences.Editor editor;
  14. Context context;
  15. int PRIVATE_MODE = 0;
  16. public static final String PREFER_NAME = "Reg";
  17. public static final String IS_USER_LOGIN = "IsUserLoggedIn";
  18. public static final String KEY_NAME = "Name";
  19. public static final String KEY_EMAIL = "Email";
  20. public static final String KEY_PASSWORD = "txtPassword";
  21.  
  22. public UserSession(Context context){
  23. this.context = context;
  24. pref = context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
  25. editor = pref.edit();
  26. }
  27.  
  28. public void createUserLoginSession(String token){
  29. editor.putBoolean(IS_USER_LOGIN, true);
  30.  
  31. editor.putString(KEY_NAME, token);
  32.  
  33. editor.commit();
  34. }
  35.  
  36. public boolean checkLogin(){
  37. if(!this.isUserLoggedIn()){
  38.  
  39. Intent i = new Intent(context, MainActivity.class);
  40.  
  41. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  42.  
  43. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  44.  
  45. context.startActivity(i);
  46.  
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
  52. public HashMap<String, String> getUserDetails(){
  53.  
  54. HashMap<String, String> user = new HashMap<String, String>();
  55.  
  56. user.put(KEY_NAME, pref.getString(KEY_NAME, null));
  57.  
  58. user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
  59.  
  60. return user;
  61. }
  62. public String getnama(){
  63. return pref.getString(KEY_NAME,"");
  64. }
  65.  
  66. public void logoutUser(){
  67.  
  68. editor.clear();
  69. editor.commit();
  70.  
  71. Intent i = new Intent(context, MainActivity.class);
  72.  
  73. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  74.  
  75. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  76.  
  77. context.startActivity(i);
  78. }
  79.  
  80. public boolean isUserLoggedIn(){
  81. return pref.getBoolean(IS_USER_LOGIN, false);
  82. }
  83.  
  84. }
Add Comment
Please, Sign In to add comment