You are on page 1of 3

How to store Single user login data for Android?

up

Referring to the question and answer on Best way to store a single user in an Android app?
vote do How does shared preferences actually work?
wn vote
What I'd like to do is:

favorite

First time user opens app adds a login id and password


Next time user opens app uses the previous id/password and data to login. (I don't want
automatic login because data in my app will be sensitive and thus even a friend taking the
mobile shouldn't be able to see it.)

Ability for the user to change this id/password


Is this possible through Shared Preferences? Or do I need to use SQLlite? I am completely new to
Android so I'd really appreciate it if you attach a working code and explanation.
java

android sqlite login sharedpreferences

share|improve this question

asked Mar 14 at 16:29

Madiha
1
Hello, three of us have tried to help below, would you be able to upvote the useful answers and tick one if it answers
your question. If it doesn't, could you clarify why so we can help further? Thanks! Neil Townsend Mar 14 at 18:46

2 Answers
activeoldestvotes

up

You can do this with shared preferences, as long as you are comfortable storing reasonably
vote d confidential data there. You will need some shared codes between storing and retrieving:
own
vote final static String pfName = "com.super.stuff.preffile.name";
final static String pfCodeForID = "com.super.stuff.pf.id";
final static String pfCodeForPassword = "com.super.stuff.pf.passwd";
final static String pfNoStringPresent = "NO-STRING-PRESENT-HERE";

final static pfCodes = MODE_PRIVATE; // See


http://developer.android.com/reference/android/content/Context.html#getShared
Preferences(java.lang.String, int)

To store the information:


String ID = //whatever;
String password = //whatever;
SharedPreferences settings = context.getSharedPreferences(pfName, pfCodes);
SharedPreferences.Editor editor = settings.edit();
editor.putString(pfCodeForID, ID);
editor.putString(pfCodeForPassword, password);
editor.commit();
To retrieve the information:
SharedPreferences settings = context.getSharedPreferences(pfName, pfCodes);
String ID = editor.getString(pfCodeForID, pfNoStringPresent);
String password = editor.getString(pfCodeForPassword, pfNoStringPresent);
if (ID.contentEquals(pfNoStringPresent) &&
password.contentEquals(pfNoStringPresent)) {
// Handle the case of nothing stored, ie get ID and password
}
Obviously this fails if both the username and the password are the same as pfNoStringPresent!
If you are concerned about storing sensitive data in this way, then you will need to store it either in a
database, or encrypt it in some way. You will need to decide how critical it is for the information to be
protected when it is being stored on a device belonging to the person who is giving you the ID
information, how important getting this information from the phone would be to a thief, etc etc.
share|improve this answer

answered Mar 14 at 16:40

Neil Townsend
2,2462524

up

Use Sqlite its fairly simple .Follow this

vote dow
n vote public SQLiteDatabase sampleDB;

sampleDB = this.openOrCreateDatabase(TABLE_NAME, MODE_PRIVATE, null);


sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
TABLE_NAME+ "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN1

+ " text not null,"+ COLUMN2


+ " text not null);");
Here i have three fields where column1 and column2 are strings having values
"user

You might also like