Here user will be provided with username and password fields at the first installation of the application. Later shared preferences will be saved and next time user will have to enter respective password to go to next activity.
This saves your info into shared preferences and later validates it.
Source code for login validation :
public class Login extends Activity implements OnClickListener {
EditText name,pass,phone;
Button login;
SharedPreferences preference;
SharedPreferences preference1;
SharedPreferences preference2;
SharedPreferences preference3;
SharedPreferences preference4;
String username;
String password;
String phone_no;
String phonetodb;
boolean bValidate;
boolean bValidate_table;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
init();
preference2 =getSharedPreferences("validate", MODE_PRIVATE);
bValidate =preference2.getBoolean("validate", false);
if(bValidate==false)
{
// For first installation of app
Toast.makeText(getApplicationContext(), "Enter details", Toast.LENGTH_LONG).show();
login.setOnClickListener(this);
}
else
{
// If you have registered here previously
// Shared preferences will display username for you
SharedPreferences preference =getSharedPreferences("userid", MODE_PRIVATE);
username = preference.getString("userid", " ");
Log.d("Username", "Username "+ username);
// Shared preferences will display phone no for you
SharedPreferences preference3 = getSharedPreferences("phone_no", MODE_PRIVATE);
phone_no = preference3.getString("phone_no", "");
// get password to match with currently entered one
SharedPreferences preference1 =getSharedPreferences("pass_word", MODE_PRIVATE);
password= preference1.getString("pass_word", " ");
Log.d("password", "password "+ password);
name.setText(username);
name.setEnabled(false);
phone.setText(phone_no);
phone.setEnabled(false);
Log.d("checking", "Check in progress");
login.setOnClickListener(this);
}
}
private void init() {
name=(EditText)findViewById(R.id.name);
login = (Button)findViewById(R.id.login);
pass=(EditText)findViewById(R.id.password);
phone = (EditText)findViewById(R.id.phone);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.login :
if(bValidate==false)
{
// Creating shared preferences at the first installation for user name
SharedPreferences preference =getSharedPreferences("userid", MODE_PRIVATE);
SharedPreferences.Editor editor1=preference.edit();
editor1.putString("userid", name.getText().toString());
editor1.commit();
String usertodb = name.getText().toString();
// Creating shared preferences at the first installation for password
SharedPreferences preference1 =getSharedPreferences("pass_word", MODE_PRIVATE);
SharedPreferences.Editor editor2=preference1.edit();
editor2.putString("pass_word", pass.getText().toString());
editor2.commit();
// Creating shared preferences at the first installation for phone no
SharedPreferences preference3 = getSharedPreferences("phone_no", MODE_PRIVATE);
SharedPreferences.Editor editor4 = preference3.edit();
editor4.putString("phone_no", phone.getText().toString());
editor4.commit();;
phonetodb = phone.getText().toString();
SharedPreferences preference2 =getSharedPreferences("validate", MODE_PRIVATE);
SharedPreferences.Editor editor3=preference2.edit();
editor3.putBoolean("validate", true);
editor3.commit();
// Starting asynchronous task to store details at server side
new LoginTask(Login.this).execute(usertodb,phonetodb);
login();
}
else
{
// matching password for authentication
String password1=pass.getText().toString();
Log.d("pass", "p "+ password1);
if(password1.equals(password))
{
Log.d("Match", "Match " + (password1.equals(password)));
login();
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(Login.this);
alert.setMessage("Incorrect password.");
alert.setTitle("Error!!!");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
alert.show();
}
}
break;
}
}
private void login()
{
preference4 =getSharedPreferences("validate_table", MODE_PRIVATE);
bValidate_table =preference4.getBoolean("validate_table", false);
if(bValidate_table==false)
{
SharedPreferences preference4 =getSharedPreferences("validate_table", MODE_PRIVATE);
SharedPreferences.Editor editor5=preference4.edit();
editor5.putBoolean("validate_table", true);
editor5.commit();
}
Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_LONG).show();
Intent j = new Intent(Login.this, RespectiveNextClass.class);
startActivity(j);
}
}
This saves your info into shared preferences and later validates it.
Source code for login validation :
public class Login extends Activity implements OnClickListener {
EditText name,pass,phone;
Button login;
SharedPreferences preference;
SharedPreferences preference1;
SharedPreferences preference2;
SharedPreferences preference3;
SharedPreferences preference4;
String username;
String password;
String phone_no;
String phonetodb;
boolean bValidate;
boolean bValidate_table;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
init();
preference2 =getSharedPreferences("validate", MODE_PRIVATE);
bValidate =preference2.getBoolean("validate", false);
if(bValidate==false)
{
// For first installation of app
Toast.makeText(getApplicationContext(), "Enter details", Toast.LENGTH_LONG).show();
login.setOnClickListener(this);
}
else
{
// If you have registered here previously
// Shared preferences will display username for you
SharedPreferences preference =getSharedPreferences("userid", MODE_PRIVATE);
username = preference.getString("userid", " ");
Log.d("Username", "Username "+ username);
// Shared preferences will display phone no for you
SharedPreferences preference3 = getSharedPreferences("phone_no", MODE_PRIVATE);
phone_no = preference3.getString("phone_no", "");
// get password to match with currently entered one
SharedPreferences preference1 =getSharedPreferences("pass_word", MODE_PRIVATE);
password= preference1.getString("pass_word", " ");
Log.d("password", "password "+ password);
name.setText(username);
name.setEnabled(false);
phone.setText(phone_no);
phone.setEnabled(false);
Log.d("checking", "Check in progress");
login.setOnClickListener(this);
}
}
private void init() {
name=(EditText)findViewById(R.id.name);
login = (Button)findViewById(R.id.login);
pass=(EditText)findViewById(R.id.password);
phone = (EditText)findViewById(R.id.phone);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.login :
if(bValidate==false)
{
// Creating shared preferences at the first installation for user name
SharedPreferences preference =getSharedPreferences("userid", MODE_PRIVATE);
SharedPreferences.Editor editor1=preference.edit();
editor1.putString("userid", name.getText().toString());
editor1.commit();
String usertodb = name.getText().toString();
// Creating shared preferences at the first installation for password
SharedPreferences preference1 =getSharedPreferences("pass_word", MODE_PRIVATE);
SharedPreferences.Editor editor2=preference1.edit();
editor2.putString("pass_word", pass.getText().toString());
editor2.commit();
// Creating shared preferences at the first installation for phone no
SharedPreferences preference3 = getSharedPreferences("phone_no", MODE_PRIVATE);
SharedPreferences.Editor editor4 = preference3.edit();
editor4.putString("phone_no", phone.getText().toString());
editor4.commit();;
phonetodb = phone.getText().toString();
SharedPreferences preference2 =getSharedPreferences("validate", MODE_PRIVATE);
SharedPreferences.Editor editor3=preference2.edit();
editor3.putBoolean("validate", true);
editor3.commit();
// Starting asynchronous task to store details at server side
new LoginTask(Login.this).execute(usertodb,phonetodb);
login();
}
else
{
// matching password for authentication
String password1=pass.getText().toString();
Log.d("pass", "p "+ password1);
if(password1.equals(password))
{
Log.d("Match", "Match " + (password1.equals(password)));
login();
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(Login.this);
alert.setMessage("Incorrect password.");
alert.setTitle("Error!!!");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
alert.show();
}
}
break;
}
}
private void login()
{
preference4 =getSharedPreferences("validate_table", MODE_PRIVATE);
bValidate_table =preference4.getBoolean("validate_table", false);
if(bValidate_table==false)
{
SharedPreferences preference4 =getSharedPreferences("validate_table", MODE_PRIVATE);
SharedPreferences.Editor editor5=preference4.edit();
editor5.putBoolean("validate_table", true);
editor5.commit();
}
Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_LONG).show();
Intent j = new Intent(Login.this, RespectiveNextClass.class);
startActivity(j);
}
}
No comments:
Post a Comment