lundi 11 janvier 2016

android Store User name and password in database and use it for multiple activities

I want to Store Username and password in Global variable or in database

actually I have a one time activity..I tried with shared preferences...

so I have given this for that..

public class Login extends Activity {
public static final String PREFS_NAME = "LoginPrefs";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    if (settings.getString("logged", "").toString().equals("logged")) {
        Intent intent = new Intent(Login.this, MainActivity.class);
        startActivity(intent);
    }

    Button b = (Button) findViewById(R.id.loginbutton);
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText username = (EditText) findViewById(R.id.login);
            EditText password = (EditText) findViewById(R.id.password);

            if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
                if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")) {

                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("logged", "logged");
                    editor.commit();
                    Intent intent = new Intent(Login.this, MainActivity.class);
                    startActivity(intent);
                }
            }
        }
    });
}

}

Inst-ed of this I want to use Global Variables so that User input will be Saved and It will continue every time.. when they open application..

Here along with User name and pass word I am saving total 4 values...

So I want to Save them as Global variables or Database and Use them In Multiple Activities...

Can any one tell with Example or correct my code... Please don't post books or guide lines tell me example link or code... where I need to change..

Aucun commentaire:

Enregistrer un commentaire