Android - 概述

Android - 会话管理

会话可以帮助您在应用程序之外存储用户数据,以便在下次用户使用您的应用程序时,您可以轻松地获取其详细信息并相应地执行.

可以通过多种方式完成.但最简单和最好的方法是通过共享首选项.

共享首选项

共享首选项允许您以密钥,值对的形式保存和检索数据.为了使用共享首选项,您必须调用getSharedPreferences()方法,该方法返回指向包含首选项值的文件的SharedPreference实例.

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

您可以使用SharedPreferences.Editor类在共享首选项中保存一些内容.您将调用SharedPreference实例的edit方法,并将在编辑器对象中接收它.它的语法是 :

Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();

除了putString方法之外,编辑器类中还有一些方法可以操作共享首选项中的数据.它们列出如下 :

Sr.No模式&描述
1

apply()

这是一种抽象方法.它会将您的更改从编辑器提交回您正在调用的sharedPreference对象

2

clear()

它将删除编辑器中的所有值

3

remove(String key)

它将删除其键作为参数传递的值

4

putLong(String key, long value)

它会在偏好编辑器中保存一个长值

5

putInt(String key,int value)

它会在首选项编辑器中保存一个整数值

6

putFloat(String key,float value)

它会在首选项编辑器中保存一个浮点值

通过共享首选项进行会话管理

按顺序要从共享首选项执行会话管理,我们需要检查 onResume 方法中共享首选项中存储的值或数据.如果我们没有数据,我们将在新安装时从头开始应用程序.但是如果我们得到了数据,我们将从用户离开的位置开始.它在下面的示例中进行了演示 :

示例

以下示例演示了会话管理的使用.它创建了一个允许您第一次登录的基本应用程序.然后当您退出应用程序而不退出时,如果再次启动应用程序,将返回到同一位置.但是,如果您从应用程序注销,您将返回到主登录屏幕.

要试验此示例,您需要在实际设备或模拟器中运行它.

步骤描述
1您将使用android studio IDE在com.example.sairamkrishna.myapplication包下创建一个Android应用程序.
2修改src/MainActivity.java文件以添加进度代码以添加会话代码.
3创建新活动并将其命名为second.java.Edit此文件以添加进度代码以添加会话代码.
4修改res/layout/activity_main.xml文件以添加相应的XML代码.
5修改res/layout/second_main.xml文件以添加respecti ve XML代码.
7运行应用程序并选择正在运行的android设备并在其上安装应用程序并验证结果.

以下是 MainActivity.java的内容.

package com.example.sairamkrishna.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
   EditText ed1,ed2,ed3;
   Button b1;
   Intent in;

   public static final String MyPREFERENCES = "MyPrefs" ;
   public static final String Name = "nameKey";
   public static final String Phone = "phoneKey";
   public static final String Email = "emailKey";
   SharedPreferences sharedpreferences;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ed1=(EditText)findViewById(R.id.editText);
      ed2=(EditText)findViewById(R.id.editText2);
      ed3=(EditText)findViewById(R.id.editText3);

      b1=(Button)findViewById(R.id.button);
      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String n  = ed1.getText().toString();
            String ph  = ed2.getText().toString();
            String e  = ed3.getText().toString();

            SharedPreferences.Editor editor = sharedpreferences.edit();

            editor.putString(Name, n);
            editor.putString(Phone, ph);
            editor.putString(Email, e);
            editor.commit();

            in = new Intent(MainActivity.this,second_main.class);
            startActivity(in);
         }
      });
   }
}

以下是 second_main.java 的内容.

package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class second_main extends Activity {
   Button bu=null;
   Button bu2=null;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.second_main);

      bu=(Button)findViewById(R.id.button2);
      bu2=(Button)findViewById(R.id.button3);
   }

   public  void logout(View view){
      SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedpreferences.edit();
      editor.clear();
      editor.commit();
   }

   public void close(View view){
      finish();
   }
}

以下是 activity_main.xml 的内容.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Shared Preference"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="35dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="67dp"
      android:hint="Name"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_below="@+id/editText"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Pass" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText3"
      android:layout_below="@+id/editText2"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Email" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="login"
      android:id="@+id/button"
      android:layout_below="@+id/editText3"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp" />

</RelativeLayout>

以下是 second_main.xml 的内容.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">
   
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Logout"
      android:onClick="logout"
      android:id="@+id/button2"
      android:layout_gravity="center_horizontal"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="191dp" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Close"
      android:onClick="close"
      android:id="@+id/button3"
      android:layout_below="@+id/button2"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="69dp" />

</RelativeLayout>

以下是 Strings.xml 的内容.

<resources>
   <string name="app_name">My Application</string>
</resources>

以下是 AndroidManifest.xml 的内容.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
      <activity android:name=".second"></activity>
      
   </application>
</manifest>

让我们尝试运行您的应用程序.我假设您在进行环境设置时创建了AVD.要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行Eclipse Run Icon icon从工具栏. Android工作室在您的AVD上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 :

Anroid会话管理教程

输入您的用户名和密码(输入您喜欢的任何内容,但记住您键入的内容),然后单击登录按钮.它显示在下面的图像中 :

Anroid Session Management Tutorial

单击登录按钮后,您将进入此欢迎屏幕.现在您的登录信息存储在共享首选项中.

Anroid会话管理教程

现在点击退出而不退出按钮,您将返回到主屏幕,优先文件输出将如下图所示

Anroid Session Management Tutorial

如果你打开myPref.xml文件作为备注文件,它将如下所示

Anroid Session Management Tutorial

如果点击退出按钮,它将删除偏好值.如果您输入不同的值作为输入,它将在XML中将这些值作为首选项输入.