vivek_Android
vivek_Android

Reputation: 1697

Using Shared Preferences in Android

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 Preference so that I can store the userid and password for the user so that user doesn't have to insert the userid and password again & again and after splash screen user directly go to welcome screen.

I read several documents about the shared preference and I came to know that there are two types of shared preference one is activity level and other one is application level.

How can I implement this?

Upvotes: 1

Views: 2738

Answers (5)

sam
sam

Reputation: 2004

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.

Upvotes: 0

Mahesh Pandit
Mahesh Pandit

Reputation: 358

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");

Upvotes: 0

Arpit Patel
Arpit Patel

Reputation: 8067

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);

Upvotes: 0

Emmanuel
Emmanuel

Reputation: 17071

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

Upvotes: 3

Macarse
Macarse

Reputation: 93173

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);

Upvotes: 3

Related Questions