我正在创建一个项目,我有一个登录屏幕,用于登录用户 应用。此登录屏幕应该只在第一次可见,因此用户可以填写并登录,但是当用户第二次打开应用程序时,应用程序必须显示main.activity。如何使用共享首选项执行此操作?
答案 0 :(得分:1)
首先,检查用户之前是否已登录。使用SharedPreferences:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
Boolean loggedIn = prefs.getBoolean("loggedIn", false);
if(loggedIn != null && loggedIn)
{
//open main activity
}else{
//open login page
}
当用户登录时,将登录信息保存到SharedPreferences:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.edit().putBoolean("loggedIn", true);
这就是你所需要的一切。
答案 1 :(得分:1)
将用户的登录信息保存在SharedPeference中:
617,5 - 52 = 565,5
565,5 - 35 = 530,5 and so on
当用户注销时,将布尔“LoggedIn”保存为false:
SharedPreferences preferences = getSharedPreferences("preference",MODE_PRIVATE);
preferences.edit().putBoolean("LoggedIn", true).apply();
在SplashActivity中,从sharedprefence获取值并调用相应的活动:
preferences.edit().putBoolean("LoggedIn", false).apply();
答案 2 :(得分:0)
如果用户登录应用时共享首选项,则希望使用共享首选项执行此操作。
mv source/files destination/
如果你想注销但是点击设置首选项..
SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
prefs.edit().putBoolean("staylogin", true).commit();
为了在没有登录屏幕的情况下启动您的应用程序,您可以在其他人之前使用启动画面并检查共享首选项...
SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
prefs.edit().putBoolean("staylogin", false).commit();
答案 3 :(得分:0)
试试这个,工作代码
在您的登录页面
// Session Manager Class
SessionManagerFor_Signin session;
private EditText EMAIL, PASSWORD;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sign_in);
// Session Manager
session = new SessionManagerFor_Signin(getApplicationContext());
login = (Button) findViewById(R.id.loginbutton);
EMAIL = (EditText) findViewById(R.id.EmaileditText1);
PASSWORD = (EditText) findViewById(R.id.passwordeditText2);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginbutton:
email = EMAIL.getText().toString();
password = PASSWORD.getText().toString();
if (email.equals("") || password.equals(""))
{
// user didn't entered user name or password
// Show alert asking him to enter the details
alert.showAlertDialog(sign_in.this, "Login Failed...","Please Enter Email and Password", false);
}
else
{
// fetch the Password form database for respective Email
storedPassword = DB.getSinlgeEntry(email);
if (password.equals(storedPassword))
{
session.createLoginSession(email, password);
Toast.makeText(sign_in.this, "Login Successfull",Toast.LENGTH_LONG).show();
Intent intent = new Intent(sign_in.this, Page1_Landing.class);
startActivity(intent);
finish();
}
}
break;
}
SessionManagerFor_Signin.java
public class SessionManagerFor_Signin
{
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "SharedPref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_EMAILID = "email";
// Email address (make variable public to access from outside)
public static final String KEY_PASSWORD = "password";
// Constructor
public SessionManagerFor_Signin(Context context)
{
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
// Create login session
public void createLoginSession(String email, String password)
{
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_EMAILID, email);
// Storing email in pref
editor.putString(KEY_PASSWORD, password);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin()
{
// Check login status
if(this.isLoggedIn())
{
Intent intent = new Intent(_context,Page1_Landing.class);
// Closing all the Activities
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Toast.makeText(getApplicationContext(),email, Toast.LENGTH_LONG).show();
_context.startActivity(intent);
}
else
{
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, MainActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
* */
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
// user mail
user.put(KEY_EMAILID, pref.getString(KEY_EMAILID, null));
// user password
user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutUser()
{
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
Intent intent = new Intent(_context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
_context.startActivity(intent);
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn()
{
return pref.getBoolean(IS_LOGIN, false);
}
}
此外,您希望参考查找以下链接