In my application, I am trying to figure out how to save userID session and use it until the user closes the app.
I know in PHP you can just use the $_SESSION['userID'] = $user_id
I want to run a query something like this in Android SQLite
SELECT user_id, username, password
FROM users
**WHERE user_id = $_SESSION[user_id]**
This is from php, is there some way I can do this in android? I heard about shared preferences but I am not sure how it works?
Sorry, I just started learning about Android programming. I am a bit of a noob. I'll appreciate if you could help me out here.
EDIT: this userID is a column that I will be calling from SQLite database and saving it as a session when the user logs in.
First you have to declare global shared preferences:
SharedPreferences sp;
then in onCreate you delcare sp:
sp = getSharedPreferences("Session", 0); //"Session" is the file name it MUST be the same in all activities that require shared preferences.
So basically when the user login you check whether he is logged in, if it is true, then save his ID in the shared preferences using:
SharedPreferences.Editor e = sp.edit();
e.putInt("ID", ID-Value-that-you-used-it-to-check-for-ID);
e.commit();
and in the login class in onCreate, you add the second block to check whether the user is logged in and he has never logged out. and simply if he is then take him to the next activity.
if (cID != 0)
{
Intent iLogged = new Intent(this, The-Next-Class.class);
finish();
startActivity(iLogged);
}
whenever you want to retrieve the value you just add...
int ID = sp.getInt("ID", 0);
and to log out the user and delete the session use the following:
sp.edit().clear().commit();
Intent iLogout = new Intent(this, Login.class);
finish();
startActivity(iLogout);
break;
Edit 2:
What you got to do is...
1) retrieve the ID from the shared preferences.. now if the UserID column is int then use.
int ID = sp.getInt("ID", 0); // 0 is default value which means if couldn't find the value in the shared preferences file give it a value of 0.
2) you have to check, if the ID != 0 then pass the value to the query...
SELECT user_id, username, password
FROM users
WHERE user_id = ID
I'm doing something similar in my android app using AppPreferences here is an example of a class i created to save an email to be used for later on in the app
public class AppPreferences {
public static final String ApplicationPreferences = "com.akada.danceworksonline.studio.MainActivity";
public String putEmail = "email";
private SharedPreferences sharedPrefs;
private SharedPreferences.Editor prefsEditor;
public AppPreferences(Context context) {
this.sharedPrefs = context.getSharedPreferences(ApplicationPreferences,
0);
this.prefsEditor = sharedPrefs.edit();
}
public String getEmailPref() {
return sharedPrefs.getString(putEmail, "");
}
public void saveEmail(String text) {
prefsEditor.putString(putEmail, text);
prefsEditor.commit();
}
and there wherever you make your call to the database have something along the lines of this .
AppPreferences _appPrefs = new AppPreferences(getApplicationContext());
then you can do something like _appPrefs.saveEmail(textviewEmail), or however you get your value.