当您在Android中集成登录功能时,会话管理是Android应用中最重要的功能之一。如果您使用的是登录功能,则在Android应用中,如果用户首次签名,则必须保存状态。然后,当用户关闭其应用程序并重新打开它时,他应该重定向到我们的主屏幕,而不是打开登录屏幕。因此,在本文中,我们将在Android应用程序中实现会话管理功能。为了实现此功能,我们正在创建一个简单的登录表单和一个主屏幕。在我们的登录表单中,用户必须输入其凭据并登录到应用程序。登录后,用户的凭据将保存在应用程序内部,并且每当他重新打开应用程序时,该用户都将被重定向到主屏幕。为了在我们的应用程序内进行会话管理,我们将使用“共享首选项”来存储用户的凭据。现在,我们将转向实施部分。
例子
我们将如上所述创建一个简单的Login应用程序,用于存储用户会话。下面提供了一个示例GIF,在其中我们将看到我们将在应用程序中构建的内容。请注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:在您的.xml字符串文件中添加以下字符串
导航至应用程序> res>值>字符串.xml,然后将以下字符串添加到其中。
XML
Session Management
Login
Enter password
Enter your Email
Logout
XML
Java
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "shared_prefs";
// key for storing email.
public static final String EMAIL_KEY = "email_key";
// key for storing password.
public static final String PASSWORD_KEY = "password_key";
// variable for shared preferences.
SharedPreferences sharedpreferences;
String email, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing EditTexts and our Button
EditText emailEdt = findViewById(R.id.idEdtEmail);
EditText passwordEdt = findViewById(R.id.idEdtPassword);
Button loginBtn = findViewById(R.id.idBtnLogin);
// getting the data which is stored in shared preferences.
sharedpreferences = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
// in shared prefs inside het string method
// we are passing key value as EMAIL_KEY and
// default value is
// set to null if not present.
email = sharedpreferences.getString(EMAIL_KEY, null);
password = sharedpreferences.getString(PASSWORD_KEY, null);
// calling on click listener for login button.
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// to check if the user fields are empty or not.
if (TextUtils.isEmpty(emailEdt.getText().toString()) && TextUtils.isEmpty(passwordEdt.getText().toString())) {
// this method will call when email and password fields are empty.
Toast.makeText(MainActivity.this, "Please Enter Email and Password", Toast.LENGTH_SHORT).show();
} else {
SharedPreferences.Editor editor = sharedpreferences.edit();
// below two lines will put values for
// email and password in shared preferences.
editor.putString(EMAIL_KEY, emailEdt.getText().toString());
editor.putString(PASSWORD_KEY, passwordEdt.getText().toString());
// to save our data with key and value.
editor.apply();
// starting new activity.
Intent i = new Intent(MainActivity.this, HomeActivity.class);
startActivity(i);
finish();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
if (email != null && password != null) {
Intent i = new Intent(MainActivity.this, HomeActivity.class);
startActivity(i);
}
}
}
Java
Java
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class HomeActivity extends AppCompatActivity {
// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "shared_prefs";
// key for storing email.
public static final String EMAIL_KEY = "email_key";
// key for storing password.
public static final String PASSWORD_KEY = "password_key";
// variable for shared preferences.
SharedPreferences sharedpreferences;
String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// initializing our shared preferences.
sharedpreferences = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
// getting data from shared prefs and
// storing it in our string variable.
email = sharedpreferences.getString(EMAIL_KEY, null);
// initializing our textview and button.
TextView welcomeTV = findViewById(R.id.idTVWelcome);
welcomeTV.setText("Welcome \n" + email);
Button logoutBtn = findViewById(R.id.idBtnLogout);
logoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling method to edit values in shared prefs.
SharedPreferences.Editor editor = sharedpreferences.edit();
// below line will clear
// the data in shared prefs.
editor.clear();
// below line will apply empty
// data to shared prefs.
editor.apply();
// starting mainactivity after
// clearing values in shared preferences.
Intent i = new Intent(HomeActivity.this, MainActivity.class);
startActivity(i);
finish();
}
});
}
}
步骤3:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。
XML格式
步骤4:为主屏幕创建新的活动
导航到应用程序> Java >应用程序的程序包名称>右键单击您的程序包名称,然后依次单击新建>活动>空活动,并确保将语言保持为Java。将活动命名为HomeActivity。
步骤5:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "shared_prefs";
// key for storing email.
public static final String EMAIL_KEY = "email_key";
// key for storing password.
public static final String PASSWORD_KEY = "password_key";
// variable for shared preferences.
SharedPreferences sharedpreferences;
String email, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing EditTexts and our Button
EditText emailEdt = findViewById(R.id.idEdtEmail);
EditText passwordEdt = findViewById(R.id.idEdtPassword);
Button loginBtn = findViewById(R.id.idBtnLogin);
// getting the data which is stored in shared preferences.
sharedpreferences = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
// in shared prefs inside het string method
// we are passing key value as EMAIL_KEY and
// default value is
// set to null if not present.
email = sharedpreferences.getString(EMAIL_KEY, null);
password = sharedpreferences.getString(PASSWORD_KEY, null);
// calling on click listener for login button.
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// to check if the user fields are empty or not.
if (TextUtils.isEmpty(emailEdt.getText().toString()) && TextUtils.isEmpty(passwordEdt.getText().toString())) {
// this method will call when email and password fields are empty.
Toast.makeText(MainActivity.this, "Please Enter Email and Password", Toast.LENGTH_SHORT).show();
} else {
SharedPreferences.Editor editor = sharedpreferences.edit();
// below two lines will put values for
// email and password in shared preferences.
editor.putString(EMAIL_KEY, emailEdt.getText().toString());
editor.putString(PASSWORD_KEY, passwordEdt.getText().toString());
// to save our data with key and value.
editor.apply();
// starting new activity.
Intent i = new Intent(MainActivity.this, HomeActivity.class);
startActivity(i);
finish();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
if (email != null && password != null) {
Intent i = new Intent(MainActivity.this, HomeActivity.class);
startActivity(i);
}
}
}
步骤6:现在我们将在主屏幕上工作
在主屏幕中,我们将显示用户的电子邮件地址和注销按钮,以便用户可以注销该应用程序。对于主屏幕,我们创建了一个名为HomeActivity的活动。导航至应用程序> res>布局> activity_home.xml并将其打开,然后将以下代码添加到其中。
Java
现在,我们将移至HomeActivity的Java文件。导航到应用程序> Java >您的应用程序的程序包名称,然后打开HomeActivity。 Java文件。在该文件内添加以下代码。
Java
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class HomeActivity extends AppCompatActivity {
// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "shared_prefs";
// key for storing email.
public static final String EMAIL_KEY = "email_key";
// key for storing password.
public static final String PASSWORD_KEY = "password_key";
// variable for shared preferences.
SharedPreferences sharedpreferences;
String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// initializing our shared preferences.
sharedpreferences = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
// getting data from shared prefs and
// storing it in our string variable.
email = sharedpreferences.getString(EMAIL_KEY, null);
// initializing our textview and button.
TextView welcomeTV = findViewById(R.id.idTVWelcome);
welcomeTV.setText("Welcome \n" + email);
Button logoutBtn = findViewById(R.id.idBtnLogout);
logoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling method to edit values in shared prefs.
SharedPreferences.Editor editor = sharedpreferences.edit();
// below line will clear
// the data in shared prefs.
editor.clear();
// below line will apply empty
// data to shared prefs.
editor.apply();
// starting mainactivity after
// clearing values in shared preferences.
Intent i = new Intent(HomeActivity.this, MainActivity.class);
startActivity(i);
finish();
}
});
}
}
输出:
查看GitHub链接: https : //github.com/ChaitanyaMunje/SessionManagementAndroid