简体   繁体   中英

Android - Saving login details

I am struggling in developing a way to have an option for the user to save their details when they login, I have searched on the internet but I have not found a solution... any help will be appreciated... here is my code

@Override
protected void onCreate(Bundle savedInstanceState) 
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     // create a instance of SQLite Database
     loginDataBaseAdapter=new LoginDataBaseAdapter(this);
     loginDataBaseAdapter=loginDataBaseAdapter.open();

     // Get The Refference Of Buttons
     btnSignIn=(Button)findViewById(R.id.buttonSignIN);
     btnSignUp=(Button)findViewById(R.id.buttonSignUP);

    // Set OnClick Listener on SignUp button 
    btnSignUp.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub

        /// Create Intent for SignUpActivity  abd Start The Activity
        Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
        startActivity(intentSignUP);
        }
    });
}
// Methos to handleClick Event of Sign In Button
public void signIn(View V)
   {
        final Dialog dialog = new Dialog(LoginActivity.this);
        dialog.setContentView(R.layout.login);
        dialog.setTitle("Login");

        // get the Refferences of views
        final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
        final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

        Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);

        // Set On ClickListener
        btnSignIn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // get The User name and Password
                String userName=editTextUserName.getText().toString();
                String password=editTextPassword.getText().toString();

                // fetch the Password form database for respective user name
                String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

                // check if the Stored password matches with  Password entered by user
                if(password.equals(storedPassword))
                {
                    startActivity(new Intent("com.example.system.HOMEACTIVITY"));
                }
                else
                {
                    Toast.makeText(LoginActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                }
            }
        });

        dialog.show();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // Close The Database
    loginDataBaseAdapter.close();
}

}

Depending on your need, you could use sharedpreference to save all the user details. And wipe it when the user log out. Or you could use a more complicated solution like the Android accountmanager. Here the link to show how it all works together.

https://udinic.wordpress.com/tag/account-manager/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM