Java 如何在用户登录Android Studio后打开应用程序进行不同的活动
我是Android Studio的新手,我正在制作一个简单的应用程序,用户必须登录,然后进入另一个屏幕。当用户第一次打开应用程序时,他们将收到登录活动的提示,如果登录成功,他们将转到另一个活动。我想知道在用户登录并关闭应用程序并再次打开应用程序后,如何将他们直接带到其他活动 以一种非常简单的方式,使用共享首选项存储用户的登录详细信息。登录前,请检查共享首选项是否有用户凭据。如果有,请切换到其他活动,否则请转到登录页面。以非常简单的方式,使用共享首选项存储用户的登录详细信息。登录前,请检查共享首选项是否有用户凭据。如果有,请切换到其他活动,否则请转到登录页面。当用户首次打开应用程序时,当登录成功时,请将变量布尔值isLogin istrue保存到共享首选项。Java 如何在用户登录Android Studio后打开应用程序进行不同的活动,java,android,android-studio,Java,Android,Android Studio,我是Android Studio的新手,我正在制作一个简单的应用程序,用户必须登录,然后进入另一个屏幕。当用户第一次打开应用程序时,他们将收到登录活动的提示,如果登录成功,他们将转到另一个活动。我想知道在用户登录并关闭应用程序并再次打开应用程序后,如何将他们直接带到其他活动 以一种非常简单的方式,使用共享首选项存储用户的登录详细信息。登录前,请检查共享首选项是否有用户凭据。如果有,请切换到其他活动,否则请转到登录页面。以非常简单的方式,使用共享首选项存储用户的登录详细信息。登录前,请检查共享首选
再次打开时,检查变量布尔值isLogin为true,然后启动MainActivity,反之亦然,则启动Activity Login。当用户首次打开应用程序时,登录成功后,将变量布尔值isLogin为true保存到共享首选项中。
再次打开时,检查变量布尔值isLogin是否为true,然后启动MainActivity,反之亦然,则启动Activity Login。在登录活动中需要类似的内容:
protected void onResume() {
super.onResume();
// Checking for user session
// if user is already logged in, take him to main activity
if (pref.isLoggedIn()) {
//here, pref is the instance of your preference manager
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
您可以创建如下首选项管理器:
public class PrefManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "YourAppName";
// All Shared Preferences Keys
private static final String KEY_IS_LOGGED_IN = "isLoggedIn";
private static final String KEY_NAME = "name";
private static final String KEY_PICTURE = "picture";
private static final String KEY_MOBILE = "mobile";
//initializing sharedPreferences
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setMobileNumber(String mobileNumber) {
editor.putString(KEY_MOBILE, mobileNumber);
editor.commit();
}
public void setName(String name) {
editor.putString(KEY_NAME, name);
editor.commit();
}
public void setPicture(String picture) {
editor.putString(KEY_PICTURE, picture);
editor.commit();
}
public String getMobileNumber() {
return pref.getString(KEY_MOBILE, null);
}
//Logging in user and setting the name and profile picture
public void createLogin(final String mobile) {
//here, handle the mobile number or email or any details that you
//use for the login. Then do this:
editor.putBoolean(KEY_IS_LOGGED_IN, true);
editor.commit();
}
public boolean isLoggedIn() {
return pref.getBoolean(KEY_IS_LOGGED_IN, false);//false is the default value in case there's nothing found with the key
}
public void clearSession() {
editor.clear();
editor.commit();
}
}
在登录活动中,登录成功后,还可以将
PrefManager
中的登录设置为true。如果您需要更多帮助,请告诉我。您在登录活动中需要类似的帮助:
protected void onResume() {
super.onResume();
// Checking for user session
// if user is already logged in, take him to main activity
if (pref.isLoggedIn()) {
//here, pref is the instance of your preference manager
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
您可以创建如下首选项管理器:
public class PrefManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "YourAppName";
// All Shared Preferences Keys
private static final String KEY_IS_LOGGED_IN = "isLoggedIn";
private static final String KEY_NAME = "name";
private static final String KEY_PICTURE = "picture";
private static final String KEY_MOBILE = "mobile";
//initializing sharedPreferences
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setMobileNumber(String mobileNumber) {
editor.putString(KEY_MOBILE, mobileNumber);
editor.commit();
}
public void setName(String name) {
editor.putString(KEY_NAME, name);
editor.commit();
}
public void setPicture(String picture) {
editor.putString(KEY_PICTURE, picture);
editor.commit();
}
public String getMobileNumber() {
return pref.getString(KEY_MOBILE, null);
}
//Logging in user and setting the name and profile picture
public void createLogin(final String mobile) {
//here, handle the mobile number or email or any details that you
//use for the login. Then do this:
editor.putBoolean(KEY_IS_LOGGED_IN, true);
editor.commit();
}
public boolean isLoggedIn() {
return pref.getBoolean(KEY_IS_LOGGED_IN, false);//false is the default value in case there's nothing found with the key
}
public void clearSession() {
editor.clear();
editor.commit();
}
}
在登录活动中,登录成功后,还可以将
PrefManager
中的登录设置为true。如果您需要更多帮助,请告诉我。启动屏幕是决定用户应该导航到哪个页面的最佳位置。在启动屏幕中,检查用户是否已登录。如果已登录,则将其导航到主页。否则将他导航到登录/注册页面。将用户登录信息存储在共享首选项中,以便在启动屏幕上进行检查。启动屏幕是决定用户应该导航到哪个页面的最佳位置。在启动屏幕中,检查用户是否已登录。如果已登录,则将其导航到主页。否则将他导航到登录/注册页面。将用户登录信息存储在共享首选项中,以便在启动屏幕上进行检查。此网站用于查询,因此,如果您在搜索后发现任何错误且未得到任何解决方案,则可以将其发布到此处。现在查看此链接,它将帮助您。使用共享首选项。此网站用于查询,因此,如果您在搜索后出现任何错误,并且没有得到任何解决方案,则可以将其发布到此处。现在查看此链接,它将帮助您。使用共享首选项。我有一个启动屏幕。好的,我在那里查一下!我有一个启动屏幕。好的,我在那里查一下!