开发者

How to presist Login credentials and do auto-login in Android

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 pasw开发者_StackOverfloword.


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


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

If this is it then I would suggest you to serialize your both the username and password after successful login.

When next time you come back to this screen deserialize the objects i.e read the stored values, and redirect user to next screen.

Here is the simple code for serialization:

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

Don't forget to deserialize when reading data. You can deserialize it similarly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜