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

左心房为你撑大大i 提交于 2020-01-16 18:54:19

问题


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..........



来源:https://stackoverflow.com/questions/5087385/how-to-presist-login-credentials-and-do-auto-login-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!