Shared Preference Android

0 votes
asked Nov 8, 2010 by vivek-android

HI All,

I have Three activities . A, B & C. Where A is a splash Activity and B Contains Login screen which consist of user Id and Password Text Field and one button to login. When i click on login it takes me to the welcome screen shows the user name on screen C.

Here i want to implement Shared Prefrence so that i can store the userid and password for the user so that user doesnt have to insert the userid and password again & again and after spllash screen user directly go to welcome screen.

i read several documents about the shared prefrence and i came to know that there are two types of shared prefrence one is activity level and other one is appplication level.

PLease help me in this case how can i implement this...its very urgent pls..

5 Answers

0 votes
answered Nov 8, 2010 by macarse

Write it from Activity A like this:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
            Editor editor = sp.edit();
            editor.putString("YOUR_KEY", "username");
            editor.commit();

You can read it afterwards with:

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
        String username = p.getString("YOUR_KEY", null);
0 votes
answered Nov 8, 2010 by emmanuel

This is relatively easy. You can store the username and password directly in the SharedPreference as follows:

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);

p.edit().putString("username", username).commit();
p.edit().putString("password", password).commit(); //SECURITY HAZARD: read below...

Then you can retrieve it like this:

String username = p.getString("username", "");
String password = p.getString("password", "");

The issue when doing this is that the password is available globally. You need to have a way to prevent others from viewing it. The way you do this is by encrypting the password when you save it and decrypting it when you load it using a symmetric key. Here's a tutorial on encryption: http://android.voxisland.com/code_examples/How_to_encrypt_and_decrypt_strings.rhtml

Let me know if this helps you at all.

Emmanuel

0 votes
answered Nov 16, 2016 by arpit-patel

This is the best way to use Shared preference just call this method

Store shared preference

public static void setDefaults(String key, String value, Context context) {
    SharedPreferences prefs =
            PreferenceManager.getDefaultSharedPreferences(context);

    SharedPreferences.Editor editor = prefs.edit();

    editor.putString(key, value);

    editor.commit();
}

Call this method and pass argument like this

Classname.setsetDefaults("key","Value",context);

Get Shared Value

    public static String getDefaults(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(key, null);
    }

Call this method And pass key

ClassName.getDefaults("Key",Context);
0 votes
answered Sep 15, 2017 by sam

Storing username and password is a bad practice instead use JWT. Take a JWT token from your response and then store it in your shared preference. If your API doesn't return any JWT in reply then at least hash your username and password before saving, but it is also unsafe.

0 votes
answered Sep 15, 2017 by mahesh-pandit

to use shared preference in android

public class SharedPref {

public static void setValue(String key, String value, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);
    editor.commit();
}

public static String getValue(String key, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(key, null);
}
 public static void setAlertDialog(Context mContext,String title,String message)
{
    AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });
    alertDialog.show();
}

}

and to set and get value from the class use following code

SharedPref.setConfig("key","value",Context);
SharedPref.getConfig("key",Context);
SharedPref.setAlertDialog(Context,"title","Content to print");
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...