Here you will get an example of android session management using SharedPreferences.
在这里,您将获得一个使用SharedPreferences进行android会话管理的示例。
If you don’t know about SharedPreferences then read my previous tutorial: Android SharedPreferences Example
如果您不了解SharedPreferences,请阅读我以前的教程: Android SharedPreferences示例
In this tutorial you will learn how to manage login session of user. The whole process works like this:
在本教程中,您将学习如何管理用户的登录会话。 整个过程如下:
-
When user enters correct username and password then the login details are stored in SharedPreferences and user is redirected to home or welcome screen. After opening home screen I have killed or finished login screen so that user can’t go back.
当用户输入正确的用户名和密码时,登录详细信息将存储在SharedPreferences中,并将用户重定向到主屏幕或欢迎屏幕。 打开主屏幕后,我已杀死或完成登录屏幕,因此用户无法返回。
-
Now even if user closes the app and open it again then he did not require to enter the login details because the details are already stored in SharedPreferences. He will be directly redirected to home screen.
现在,即使用户关闭了该应用程序并再次打开它,他也不需要输入登录详细信息,因为该详细信息已存储在SharedPreferences中。 他将被直接重定向到主屏幕。
-
When user click on logout button, the data stored in SharedPreferences is deleted and login session is destroyed. The user is redirected to login screen and home screen is killed so that he can’t go back.
当用户单击注销按钮时,存储在SharedPreferences中的数据将被删除,并且登录会话将被破坏。 用户被重定向到登录屏幕,主屏幕被杀死,因此他无法返回。
In this way user login session is managed. Session management concept is very important and frequently used while developing any android app.
这样,可以管理用户登录会话。 会话管理概念非常重要,在开发任何android应用程序时都经常使用。
Below I have shared an example that will help you to implement it in your app.
下面我分享了一个示例,该示例将帮助您在应用程序中实现它。
使用SharedPreferences的Android会话管理 (Android Session Management Using SharedPreferences)
Create a new project with package name com.sessionmanagement.
用包名称com.sessionmanagement创建一个新项目。
Now create two blank activities with name MainActivity and Home. By default you may get MainActivity, in that case you have to create only Home activity.
现在,创建两个名为MainActivity和Home的空白活动。 默认情况下,您可能会获得MainActivity ,在这种情况下,您仅需创建Home活动。
Now add following code in respective files.
现在,在相应文件中添加以下代码。
Note: Here I have used programmer as username and password. You can change the login details according to you or you can get the details from server or database and then compare with it.
注意:这里我使用程序员作为用户名和密码 。 您可以根据自己的喜好更改登录详细信息,也可以从服务器或数据库获取详细信息,然后与之进行比较。
MainActivity.java (MainActivity.java)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package com.sessionmanagement;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText username,password;
Button button;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
button=(Button)findViewById(R.id.button);
sp=getSharedPreferences("login",MODE_PRIVATE);
//if SharedPreferences contains username and password then directly redirect to Home activity
if(sp.contains("username") && sp.contains("password")){
startActivity(new Intent(MainActivity.this,Home.class));
finish(); //finish current activity
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginCheck();
}
});
}
void loginCheck(){
//check username and password are correct and then add them to SharedPreferences
if(username.getText().toString().equals("programmer") && password.getText().toString().equals("programmer")){
SharedPreferences.Editor e=sp.edit();
e.putString("username","programmer");
e.putString("password","programmer");
e.commit();
Toast.makeText(MainActivity.this,"Login Successful",Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,Home.class));
finish();
}
else{
Toast.makeText(MainActivity.this,"Incorrect Login Details",Toast.LENGTH_LONG).show();
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package com . sessionmanagement ;
import android . app . Activity ;
import android . content . Intent ;
import android . content . SharedPreferences ;
import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . EditText ;
import android . widget . Toast ;
public class MainActivity extends Activity {
EditText username , password ;
Button button ;
SharedPreferences sp ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
username = ( EditText ) findViewById ( R . id . username ) ;
password = ( EditText ) findViewById ( R . id . password ) ;
button = ( Button ) findViewById ( R . id . button ) ;
sp = getSharedPreferences ( "login" , MODE_PRIVATE ) ;
//if SharedPreferences contains username and password then directly redirect to Home activity
if ( sp . contains ( "username" ) && sp . contains ( "password" ) ) {
startActivity ( new Intent ( MainActivity . this , Home . class ) ) ;
finish ( ) ; //finish current activity
}
button . setOnClickListener ( new View . OnClickListener ( ) {
@Override
public void onClick ( View v ) {
loginCheck ( ) ;
}
} ) ;
}
void loginCheck ( ) {
//check username and password are correct and then add them to SharedPreferences
if ( username . getText ( ) . toString ( ) . equals ( "programmer" ) && password . getText ( ) . toString ( ) . equals ( "programmer" ) ) {
SharedPreferences . Editor e = sp . edit ( ) ;
e . putString ( "username" , "programmer" ) ;
e . putString ( "password" , "programmer" ) ;
e . commit ( ) ;
Toast . makeText ( MainActivity . this , "Login Successful" , Toast . LENGTH_LONG ) . show ( ) ;
startActivity ( new Intent ( MainActivity . this , Home . class ) ) ;
finish ( ) ;
}
else {
Toast . makeText ( MainActivity . this , "Incorrect Login Details" , Toast . LENGTH_LONG ) . show ( ) ;
}
}
}
|
activity_main.xml (activity_main.xml)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:paddingBottom="15dp" tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Login"
android:textSize="40dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="Enter Username"
android:id="@+id/username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Enter Password"
android:id="@+id/password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:id="@+id/button"/>
</LinearLayout>
|
家.java (Home.java)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com.sessionmanagement;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Home extends Activity {
Button logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
logout=(Button)findViewById(R.id.logout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp=getSharedPreferences("login",MODE_PRIVATE);
SharedPreferences.Editor e=sp.edit();
e.clear();
e.commit();
startActivity(new Intent(Home.this,MainActivity.class));
finish(); //finish current activity
}
});
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com . sessionmanagement ;
import android . app . Activity ;
import android . content . Intent ;
import android . content . SharedPreferences ;
import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
public class Home extends Activity {
Button logout ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_home ) ;
logout = ( Button ) findViewById ( R . id . logout ) ;
logout . setOnClickListener ( new View . OnClickListener ( ) {
@Override
public void onClick ( View v ) {
SharedPreferences sp = getSharedPreferences ( "login" , MODE_PRIVATE ) ;
SharedPreferences . Editor e = sp . edit ( ) ;
e . clear ( ) ;
e . commit ( ) ;
startActivity ( new Intent ( Home . this , MainActivity . class ) ) ;
finish ( ) ; //finish current activity
}
} ) ;
}
}
|
activity_home.xml (activity_home.xml)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:paddingBottom="15dp" tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Home"
android:textSize="40dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="-- Welcome --"
android:textSize="30dp"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Logout"
android:layout_marginTop="20dp"
android:id="@+id/logout"/>
</LinearLayout>
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<LinearLayout xmlns : android = "http://schemas.android.com/apk/res/android"
xmlns : tools = "http://schemas.android.com/tools" android : layout_width = "match_parent"
android : layout_height = "match_parent" android : paddingLeft = "15dp"
android : paddingRight = "15dp"
android : paddingTop = "15dp"
android : paddingBottom = "15dp" tools : context = ".MainActivity"
android : orientation = "vertical" >
<TextView
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : gravity = "center"
android : text = "Home"
android : textSize = "40dp" />
<TextView
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : gravity = "center"
android : text = "-- Welcome --"
android : textSize = "30dp"
android : layout_marginTop = "10dp" />
<Button
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : textSize = "20dp"
android : text = "Logout"
android : layout_marginTop = "20dp"
android : id = "@+id/logout" />
</LinearLayout>
|
Finally run and test your app.
最后运行并测试您的应用程序。
Output
输出量
Comment below if you have any queries related to above android session management tutorial.
如果您对以上android会话管理教程有任何疑问,请在下面评论。
Happy Coding!! ???? ????
快乐编码! ????
翻译自: https://www.thecrazyprogrammer.com/2016/02/android-session-management-using-sharedpreferences.html