How to presist Login credentials and do auto-login

2019-08-23 08:42发布

问题:

How can I save userId and Password?

I am developing an application which requires to store email id and password, so that the user can directly redirect to his home page if the user already exists.

Now Next time when the user logins, he is directed to his home page directly. He should not again type in his userId and pasword.

回答1:

You can stream multiple objects to the same file

FileOutputStream fStream = openFileOutput(namefile.bin, Context.MODE_PRIVATE) ;
ObjectOutputStream oStream = new ObjectOutputStream(fStream);
oStream.writeObject(Username) ;
oStream.writeObject(Password) ;
oStream.flush() ;
oStream.close() ;


回答2:

First thing i wud like 2 tell you is that try 2 describe your questions comlpletely. Make it more elaborative. So that more people can help you.......

i think your question would be how to save username and password and redirect user 2 next screen....

if this is it then i would suggest you 2 serealize your both the username and password after successfull login...

when next time u come back 2 this screen deserealize the objects i.e read the stored values.. and redirect user 2 next screen..

here is the simple code for serealization...

public void serializeCredentials(String Username,String Password) {
            try {
                FileOutputStream fStream = openFileOutput(namefile.bin, Context.MODE_PRIVATE) ;
                ObjectOutputStream oStream = new ObjectOutputStream(fStream);
                oStream.writeObject(Username) ;
                FileOutputStream fos = openFileOutput(passwordfile.bin, Context.MODE_PRIVATE) ;
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(Password) ;
                oStream.flush() ;
                oStream.close() ;
                oos.flush() ;
                oos.close() ;
                Log.v("Serialization success", "Success");
            } catch (Exception e) {

                Log.v("IO Exception", e.getMessage());
            }
            }   

thanks. dont foreget 2 deserialize when reading data. you can deserialize it similarly..........