android
android

Reputation: 386

shared preference in android

I am new in android development.

I've an activity in which I'm taking user name and password.and I'm passing those values to a web service which returns a key as a response.i have one toggle button in my activity. now if the user checks the toggle button that means he want to keep logged in and the user should be redirected to next activity when he next time log-in.

If toggle button is checked I'm storing user name, password and key in shared preference. but I'm not getting how to retrieve those details next time(i.e when user next time log-in)

Upvotes: 2

Views: 5610

Answers (6)

Prashant Solanki
Prashant Solanki

Reputation: 660

Try this https://prashantsolanki3.github.io/Secure-Pref-Manager/ for saving and retrieving shared preferences securely and with ease.

The keys and values are automatically encrypted.

Save:

SecurePrefManager.with(this)
            .set("user_name")
            .value("LoremIpsum")
            .go();

Retrieve:

String userName = SecurePrefManager.with(this)
            .get("user_name")
            .defaultValue("unknown")
            .go();

Hope this helps!

Upvotes: 0

Taruni
Taruni

Reputation: 1671

Then he can encrypt the password and store it in sharedpreferences whenever he needs the password he can get that encrypted password from the sharedpreferences and decrypt it.

Upvotes: 0

Dharmendra
Dharmendra

Reputation: 33996

userDetails = this.getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putString("username", txtUname.getText().toString().trim());
edit.putString("password", txtPass.getText().toString().trim());
edit.commit();
Toast.makeText(this, "Login details are saved..", 3000).show();

this way you can fetch preference

String Uname = userDetails.getString("username", "");
String pass = userDetails.getString("password", "");

and check for login this way

if(Uname=="" && pass =="")
//Go to login
else
//Go to Next Activity

try like this

best of luck

Upvotes: 6

PravinCG
PravinCG

Reputation: 7708

Based on the Checkbox add a sharedPreferences Boolean value when the user checks/unchecks on keep logged in. Then as you call onCreate of Login activity you need to check this and call the next activity if the boolean value is true.

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

try this for store value in sharePreferences..

SharedPreferences prefs = getSharedPreferences("Share", Context.MODE_PRIVATE );
Editor editor = prefs.edit();
editor.putInt("Value", 1 );
editor.commit();

for get value

prefs.getInt("Value",0);

/////////////////////////////////////////

String Uname = userDetails.getString("username", "");
String pass = userDetails.getString("password", "");

if(Uname=="" && pass =="")
//Go to login
else
//Go to Next Activity

Upvotes: 2

JAL
JAL

Reputation: 3319

Avoid storing the user password. For each user, consider storing:

the user id
a random seed unique to this user 
a hash of the seeded password. 

The user will need to reenter his password, then you can add the stored seed to the entered password and apply the hash algorithm X times to the seeded password. Then compare the hash to the stored hash.

Upvotes: 0

Related Questions