I need to save the user session, my app is connected to a WebService , I have tried to implement SharedPreferences to create a "session" , but it has not worked. If someone could guide me or have a better idea.
I save the username and password after he Asynctask
validates the data
if(result.equalsIgnoreCase("true"))
{
final String usuario = editusuario.getText().toString();
final String clave= editpass.getText().toString();
Toast.makeText(getApplicationContext(),"Bienvenido: "+usuario,Toast.LENGTH_SHORT).show();
session.createLoginSession(usuario,clave ); // aqui intento guerdar los datos para sesión
Intent intent = new Intent(MainActivity.this,cronometro.class);
startActivity(intent);
MainActivity.this.finish();
I reference within the class that contains theAsyntask
session = new SessionManager(getApplicationContext());
And the class SessionManager
is:
public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "reg";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_NAME = "Name";
public static final String KEY_EMAIL = "Email";
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String Name, String Email){
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_NAME, Name);
editor.putString(KEY_EMAIL, Email);
editor.commit();
}
public void checkLogin(){
if(!this.isLoggedIn()){
Intent i = new Intent(_context, cronometro.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_NAME, pref.getString(KEY_NAME, null));
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
return user;
}
public void logoutUser(){
editor.clear();
editor.commit();
Intent i = new Intent(_context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
The problem is that as I pass the username and password of the server so that it can compare the saved ones.
NOTE: I HAVE CHANGED THE APPROACH TO THE PROBLEM, I HAVE ADDED A POSSIBLE SOLUTION IN THE ANSWERS SESSION
I have found the solution, I have approached the problem in a simpler way, here my answer in case another user needs it. I want to emphasize that any security or code improvements are welcome.
First of all, I add a
Checkbox
to my LOGIN form to then create two functions for this control, the first one I call,guerdar_estado_boton
which is the following:This function simply saves the state of
Checkbox
whether or not it is "activated".Then I create a second function called
obtener_valor_boton
which is the following:This function I obtain the status of
Checkbox
whether it is activated or not, and by default I pass afalse
, that is, it will not save the user's sessionAhhh and the static variables are:
Once you have all this functional, in the LOGIN activity before everything else inside the activity is executed, use the following:
This means that before anything else is executed, it will verify if
checkbox
it was saved as activated or not and if it is true, it will go directly to the next activity, without going through the LOGIN .Ahhh it escaped me I execute the function
guardar_estado_boton()
; onAsynctask
after successfully validating and logging in with the server.