Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. public class User{
  2. /**
  3. * Propriedades do Objeto
  4. */
  5. private String nome;
  6. private String login;
  7. private String senha;
  8. private Long timestamp;
  9.  
  10. /**
  11. * Contrutor que carrega as informações
  12. * @param nome
  13. * @param login
  14. * @param senha
  15. * @param timestamp
  16. */
  17. public User(final String nome, final String login, final String senha, final Long timestamp){
  18. this.nome = nome;
  19. this.login = login;
  20. this.senha = senha;
  21. this.timestamp = timestamp;
  22. }
  23.  
  24. /**
  25. * Construtor padrão
  26. */
  27. public User(){ }
  28.  
  29. /**
  30. * Getter's and Setter's
  31. */
  32.  
  33. public String getNome() {
  34. return nome;
  35. }
  36. public void setNome(String nome) {
  37. this.nome = nome;
  38. }
  39. public Long getTimestamp() {
  40. return timestamp;
  41. }
  42. public void setTimestamp(Long timestamp) {
  43. this.timestamp = timestamp;
  44. }
  45. public String getSenha() {
  46. return senha;
  47. }
  48. public void setSenha(String senha) {
  49. this.senha = senha;
  50. }
  51. public String getLogin() {
  52. return login;
  53. }
  54. public void setLogin(String login) {
  55. this.login = login;
  56. }
  57. }
  58.  
  59. /**
  60. * Constantes utilizadas para salvar / resgatar os dados
  61. */
  62. private String USER = "#USER";
  63. private String NAME = "#name";
  64. private String LOGIN = "#login";
  65. private String PASSWORD = "#passWord";
  66. private String TIMESTAMP = "#timestamp";
  67. /**
  68. * Coleta os dados de SharedPreferences e retorna no objeto
  69. * @param mContext
  70. * @return User
  71. */
  72.  
  73. public User getUser(final Context mContext){
  74. if(null == mContext) return null;
  75. //Cria uma instancia do SharedPreferences
  76. final SharedPreferences prefs = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE);
  77. // se for nulo, n˜ao temos mais o que fazer, e retornamos nulo!
  78. if(null == prefs) return null;
  79.  
  80. /**
  81. * Cria uma nova instacia e coleta os valores!
  82. * Para carregar um valor passamos o nome da Propriedade e um valor padrão.
  83. * Se não haver dados para esta propriedade, ele irá retornar o valor padão
  84. */
  85.  
  86. final User user = new User();
  87. user.setLogin(prefs.getString(LOGIN, null));
  88. user.setNome(prefs.getString(NAME, null));
  89. user.setSenha(prefs.getString(PASSWORD, null));
  90. user.setTimestamp(prefs.getLong(TIMESTAMP, 0L));
  91. return user;
  92. }
  93.  
  94.  
  95. /**
  96. * Grava as informações do objeto em um SharedPreferences.
  97. * @param user
  98. * @param mContext
  99. */
  100. public void setUser(final User user, final Context mContext){
  101. if(null == user) return;
  102. //Cria uma instancia do SharedPreferences
  103. SharedPreferences prefs = mContext.getSharedPreferences(USER, Context.MODE_PRIVATE);
  104. // Criamos um instancia do editor, para salvamos os dados
  105. SharedPreferences.Editor editor = prefs.edit();
  106. editor.putString(LOGIN, user.getLogin());
  107. editor.putString(NAME, user.getNome());
  108. editor.putString(PASSWORD, user.getSenha());
  109. // para que sempre atualize, passamos o valor do Sistema.
  110. editor.putLong(TIMESTAMP, System.currentTimeMillis());
  111. // Para que as informações sejam atualizadas
  112. editor.apply();
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement