Soulforth
Soulforth

Reputation: 55

Keeping user logged on, on Android an android app

Hey guys i am currently working on an android app that will allow me to login with a registered user and then post how much he heighs, his collesterol level and so on, trough web services using ksoap, and i don't know how to keep the user logged on trough a web service like ksoap for then to introduce the values. I don't have code yet i am just trying to figure out how am i going to do it, because in android i guess you could say i am still taking baby steps. The website where the info will be posted is already created but i need to know how i keep the user logged in so that then i can put his numbers and send trough the web services to the site db the right values to the right user.

Upvotes: 1

Views: 2081

Answers (2)

Chirag
Chirag

Reputation: 56925

Well , you can save boolean variable in Shared Preference when user logged in . So when ever you want to send data you need to check the value of that boolean variable . if its true then you can send data otherwise redirect to login page.

When user logged out then you need to set false to that variable in shared preference or clear shared preference.

Example

public class PreferenceData 
{
    static final String PREF_USER_ID = "user_logged_in";

    public static SharedPreferences getSharedPreferences(Context ctx) 
    {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    public static void setUserLoggedIn(Context ctx, boolean userLoggedIn) 
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putBoolean(PREF_USER_ID, userLoggedIn);
        editor.commit();
    }

    public static boolean getUserLoggedIn(Context ctx) 
    {
        return getSharedPreferences(ctx).putBoolean(PREF_USER_ID, false);
    }
}

Upvotes: 2

Stian Storrvik
Stian Storrvik

Reputation: 2199

well, you can save a token with a timestamp in the sqlite db or flash memory and send it as a parameter in your request. when you check if the login token exists, you can check towards timestamp if you want it to time out - create a logout function that clears this token

Upvotes: 0

Related Questions