简体   繁体   中英

Using Android SharedPreferences or Internal Storage to store values of EditText fields locally

I'm currently doing a project in college and we are creating and app for students.

Part of the app is easing access to the student email as our current college website is a bit all over the place.

Basically what I am trying to do is store a users email and password that they entered into the EditTexts that I have.

The data should only be stored if the user selects the option to store the data.

Also if the user's already have stored data it shall skip the page with the login and proceed straight to the Webview.

Does anyone have any examples or any tips on how to do this?

I looked at a few different ways to do it but none of the explanations seemed pretty straight forward and a lot of tutorials are using values that I do not know how to convert.

*Do note that I am relatively new to Android development as I only took it on this year!

Thank you!

TJ

You can save it into sharedPreferences. You can do it this way:

/** Constant to identify your sharedPref */
public static final String MY_SHARED_PREFERENCES    = "mySharedPrefInstance";
public static final String USER_TAG                 = "sharedPrefsUserTag";

// Get shared preferences
SharedPreferences sharedPref = getSharedPreferences(MY_SHARED_PREFERENCES, MODE_PRIVATE);

// Check if sharedPreferences already contain this user
if (sharedPref.getString(USER_TAG) == textView.gettext()) {
    // The user is already saved
} else {
    // Save a string obj
sharedPref.edit().putString(USER_TAG, editTextUser.getText());
// Save password too
sharedPref.edit().putString(PWD_TAG, editTextPwd.getText());

// Maybe, you would to encrypt the pwd (I recommend this) 
// So, for example:
sharedPref.edit().putString(PWD_TAG, MD5( editTextPwd.getText() ) );

// Commit changes
sharedPref.commit();

}

/** Encrypt params string with MD5 algorithm */
public static String MD5(String md5) {
    try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) { }
    return null;
}

If you provide your code, it's easily to help you better! Hope this help.

It would be best to use a SQLite database on the device. This way it can easily be expanded by adding tables or columns accordingly if the application expands in it's needs. It can also then be used to store multiple accounts so that you could do validation on the login on the device itself.

Take a look at these examples for help in using a SQLite database :

Example 1 with Vogella

Example 2 with Android Hive

If you take a look at Android Hive first, it gives a good explanation as to how it works, were as Vogella shows you exactly how to implement it into your code.

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