我正在开发需要登录的 android 应用程序。我想检查用户何时按下 PostIdea 活动按钮,然后检查用户是否登录,如果没有,则重定向到登录页面,否则继续执行任务。
在这里,我为此使用 SharedPreferences。现在,当我按下登录按钮时,将用户名和密码添加到 SharedPreferences 中,当按下 PostIdea Activity 时,我得到了用户名和密码。
但我的问题是,当我重新启动应用程序并在没有登录的情况下进入 PostIdea 活动时,它会记住 SharedPreferences 数据的最后一次登录(用户名/密码)。如何从 SharedPreferences 中删除该数据,以便我的应用程序正常工作。让我知道我在哪里做错了,或者我的问题还有其他解决方案。
谢谢。
登录活动类
public class LoginActivity extends CheerfoolznativeActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setHeader("Login");
editUser = (EditText) findViewById(R.id.login_useredit);
editPass = (EditText) findViewById(R.id.login_passedit);
txterror = (TextView) findViewById(R.id.login_error);
textlogin = (TextView) findViewById(R.id.login_postidea_textView);
btngotoregister = (Button) findViewById(R.id.login_btnLinkToRegisterScreen);
btnlogin = (Button) findViewById(R.id.login_button);
pgb = (ProgressBar) findViewById(R.id.login_progressBar);
btnlogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Login().execute();
}
});
}
public class Login extends AsyncTask<Void, Void, Void> {
int i = 0;
String uName ;
String Password;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
uName = editUser.getText().toString().trim();
Password = editPass.getText().toString().trim();
if (uName.equals("") | Password.equals("")) {
Toast.makeText(getApplicationContext(), "Enter the Data",
Toast.LENGTH_SHORT).show();
if (editUser.length() == 0) {
editUser.setError("Enter Username");
}
if (editPass.length() == 0) {
editPass.setError("Enter Password");
}
} else {
pgb.setVisibility(View.VISIBLE);
}
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String loginURL = "http://www.cheerfoolz.com/rest/user/login";
strResponse = util.makeWebCall(loginURL, uName, Password);
return null;
}
@Override
public void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
txterror.setText("");
try {
if (strResponse.substring(KEY_SUCCESS) != null) {
txterror.setText("");
// SharedPreferences Logic
SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putString("username", uName);
edit.putString("password", Password);
edit.commit();
new FetchUserProfileTask().execute();
} else {
txterror.setText("Username and Password Not valid !!!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class FetchUserProfileTask extends AsyncTask<Void, Void, Void> {
int i = 0;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
//logic for the featch user profile data
return null;
}
@Override
public void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pgb.setVisibility(View.GONE);
displayProfile();
}
}
public void displayProfile() {
Intent in1 = new Intent(LoginActivity.this, post_myprofile.class);
in1.putExtra("loginObject", bean);
startActivity(in1);
}
}
Post_idea_Activity.class
public class Post_idea_Activity extends CheerfoolznativeActivity {
TextView txtwelcome;
String Uname="";
String pass = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
System.out.println("value of the userdertails ==========>"+ userDetails);
//String Uname="";
//String pass= "";
Uname = userDetails.getString("username", "");
pass = userDetails.getString("password", "");
Toast.makeText(getApplicationContext(),"username : "+Uname +" \n password :"+pass, Toast.LENGTH_SHORT).show();
if (Uname.equals(null))
{
Intent in1 = new Intent(this, LoginActivity.class);
in1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in1);
}
else {
setContentView(R.layout.post_idea);
setHeader("Post idea");
txtwelcome =(TextView)findViewById(R.id.post_welcome_text);
}
}
// coding for the layout
}