简体   繁体   中英

How to store Single user login data for Android?

Referring to the question and answer on Best way to store a single user in an Android app?

How does shared preferences actually work?

What I'd like to do is:

  • 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.

You can do this with shared preferences, as long as you are comfortable storing reasonably confidential data there. You will need some shared codes between storing and retrieving:

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#getSharedPreferences(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.

Use Sqlite, its fairly simple. Follow this:

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 "username" and "password". After this creation you can execute query as what ever you need.

Integrate with AccountManager then use setUserData for it...the best way i think. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM