2

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)

user229044
  • 232,980
  • 40
  • 330
  • 338
android
  • 386
  • 1
  • 8
  • 21

6 Answers6

6
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

Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • It's not really wise to store password in shared preferences - otherwise it's ok – Barmaley May 16 '11 at 05:11
  • @Dr.Jackal..Thanks for Ur quick reply sir...but I'm through up to this...now I'm not able to understand that when user next time log-in ,how to redirect to the next activity... – android May 16 '11 at 05:21
  • @android you can check username and password from your preference if its already contains username and password then you can redirect to your activity else you can redirect to log in page – Dharmendra Aug 04 '11 at 03:36
2

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
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
0

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!

Prashant Solanki
  • 660
  • 5
  • 10
0

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
Taruni
  • 1,671
  • 4
  • 22
  • 43
0

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.

JAL
  • 3,319
  • 2
  • 20
  • 17
0

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.

PravinCG
  • 7,688
  • 3
  • 30
  • 55