I am trying the code to have login credentials saved using SharedPreferences. I am able or believe the credentials are being saved successfully but when trying to login by comparing the credentials in edit to the ones saved. I keep receiving a "Wrong Password" error. Not sure what I am overlooking. the code to login is below.
Login:
public class AccessApp extends Activity implements OnClickListener {
private SharedPreferences sp;
String user,pass;
Button lBttn,cBttn;
EditText uname,pword;
Intent i;
int flag=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lBttn=(Button)findViewById(R.id.login_button);
cBttn=(Button)findViewById(R.id.cancel_button);
uname=(EditText)findViewById(R.id.username);
pword=(EditText)findViewById(R.id.password);
lBttn.setOnClickListener(this);
cBttn.setOnClickListener(this);
}
public void onClick(View arg0) {
sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE);
user=sp.getString("USERNAME", "");
pass=sp.getString("PASSWORD","");
if(lBttn==arg0){
if((uname.getText().toString().compareTo(user)==0)&&
(pword.getText().toString().compareTo(pass)==0))
{
Toast.makeText(this, "You are Logged In", 20000).show();
Intent intent;
intent=new Intent(this,details.class);
startActivity(intent);
flag=1;
}
else
{
Toast.makeText(this, "Wrong Username or Password",20000).show();
flag=0;
}
}
Register:
public class SharedPrefLoginActivity extends Activity implements OnClickListener {
private SharedPreferences sp;
Intent i;
Button regBttn,rtnBttn;
EditText rName,rPwd;
String user,pass,cpass,chk;
String stat="a";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
rName=(EditText)findViewById(R.id.reg_uname);
rPwd=(EditText)findViewById(R.id.reg_pswd);
regBttn=(Button)findViewById(R.id.reg_button);
rtnBttn=(Button)findViewById(R.id.rtn_button);
regBttn.setOnClickListener(this);
rtnBttn.setOnClickListener(this);
sp=this.getSharedPreferences("AccessApp", MODE_WORLD_READABLE);
chk=sp.getString("USERNAME", "");
if(chk.length()!=0){
sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
i=new Intent(this,AccessApp.class);
startActivity(i);
}
}
public void onClick(View arg0) {
user=rName.getText().toString();
pass=rPwd.getText().toString();
if(arg0==regBttn){
if((user.length()!=0))
{
if((pass.length()!=0))
{
sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
Editor myEditor=sp.edit();
myEditor.putString("USERNAME", user);
myEditor.putString("PASSWORD", pass);
myEditor.commit();
Toast.makeText(this, "Registration is successfull",10000).show();
i=new Intent(this,AccessApp.class);
startActivity(i);
}
else
{
Toast.makeText(this, "Please Enter password", 10000).show();
}
}
else{
Toast.makeText(this,"Please Enter Username",10000).show();
}
}
The issue causing the issue is below
login:
sp=this.getSharedPreferences("Register", MODE_WORLD_READABLE); /* change "Register" to "AccessApp" */
registration:
sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);