Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. package com.alilozano.chatbazinga;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6.  
  7. import com.alilozano.chatbazinga.utils.BazingaPreferences;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10. private BazingaPreferences prefs;
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. prefs = new BazingaPreferences(this);
  17. String currentUser = prefs.getString(BazingaPreferences.USUARIO_LOGUEADO, null);
  18. if(currentUser == null){
  19. Intent intent = new Intent(this, LoginActivity.class);
  20. startActivity(intent);
  21. }
  22. }
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. package com.alilozano.chatbazinga;
  66.  
  67. import android.content.Intent;
  68. import android.support.v7.app.AppCompatActivity;
  69. import android.os.Bundle;
  70. import android.view.View;
  71. import android.widget.Button;
  72. import android.widget.EditText;
  73. import android.widget.Toast;
  74.  
  75. import com.alilozano.chatbazinga.utils.BazingaPreferences;
  76.  
  77. public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
  78. private BazingaPreferences prefs;
  79. private Button btnLogin;
  80. private EditText txtUsuario;
  81. private EditText txtPassword;
  82. @Override
  83. protected void onCreate(Bundle savedInstanceState) {
  84. super.onCreate(savedInstanceState);
  85. setContentView(R.layout.activity_login);
  86. prefs = new BazingaPreferences(this);
  87. btnLogin = findViewById(R.id.btnLogin);
  88. txtUsuario = findViewById(R.id.txtUsuario);
  89. txtPassword = findViewById(R.id.txtPassword);
  90. btnLogin.setOnClickListener(this);
  91. }
  92.  
  93. @Override
  94. public void onClick(View view) {
  95. if(txtUsuario.getText().toString().equals("admin")
  96. && txtPassword.getText().toString().equals("admin")){
  97. Intent intent = new Intent(this, MainActivity.class);
  98. startActivity(intent);
  99. prefs.setString(BazingaPreferences.USUARIO_LOGUEADO, "admin");
  100. }else{
  101. Toast.makeText(this,
  102. "Usuario/Contraseña incorrecta", Toast.LENGTH_SHORT).show();
  103. }
  104. }
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. package com.alilozano.chatbazinga.utils;
  133.  
  134. import android.content.Context;
  135. import android.content.SharedPreferences;
  136.  
  137. public class BazingaPreferences {
  138. private SharedPreferences prefs;
  139. private static String PREFERENCES_NAME="BazingaPreferences";
  140. public static String USUARIO_LOGUEADO="USUARIO_LOGUEADO";
  141. public BazingaPreferences(Context ctx) {
  142. prefs = ctx.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
  143. }
  144. public String getString(String key, String default_){
  145. return prefs.getString(key, default_);
  146. }
  147. public void setString(String key, String value){
  148. SharedPreferences.Editor editor = prefs.edit();
  149. editor.putString(key, value);
  150. editor.apply();
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement