-1

I am developing an Android App. It has a login page.. what is the best way to save users' details so they do not have to fill in their credentials every time they use the app.. according to Link ? we can use account manager or oath2. I am still not sure of which methods i can use.. I simply want the user to enter a username and a password once.. I came across other methods but I am looking for the best method to be used.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335

1 Answers1

2

You can use this function to save username and password

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
        if(shared.contains("username") && shared.contains("password")){
            startingActivity();
        } else {
            saveInformation(userId,pass);
        }
    }


public void saveInformation(String username,String password) {
        SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
        SharedPreferences.Editor editor = shared.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.commit();
    }
Developer
  • 6,292
  • 19
  • 55
  • 115
  • 1
    Be cautious when saving passwords into sharedPreferences as they are stored within a plain text xml file; the above method works but will store it in plain text. Users with rooted devices can access this file and view the plain text password. See this page for a good overview of methods to protect data within your shared preferences: http://www.codeproject.com/Articles/549119/Encryption-Wrapper-for-Android-SharedPreferences – matthewrdev Sep 12 '13 at 05:29
  • @MattR thank you very much for this precious comment and thank you for helping me to learn more thanks alot – Developer Sep 12 '13 at 05:31
  • @MattR where i will find this shared preferences XML file in my device – Developer Sep 12 '13 at 06:30
  • Gaurav, see http://stackoverflow.com/questions/6146106/where-are-shared-preferences-stored – matthewrdev Sep 12 '13 at 08:17
  • You will need root privileges to view/access this folder though. – matthewrdev Sep 12 '13 at 08:17
  • @MattR i have seeen that post i want to see my shared pref data in the device i am not getting where i will get this – Developer Sep 12 '13 at 09:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37240/discussion-between-gaurav-and-matt-r) – Developer Sep 12 '13 at 09:04