安卓利用SharedPreferences保存登录信息,防止重复登录

  项目需求:每次退出程序再次启动时都需要重新进行登录,现在需要仿照微信,实现每次启动后自动登录。

  项目用到的三个界面:WelcomeActivity(欢迎界面),MainActivity(登录界面),StudentMainActivity(真正的主界面),需要实现的效果是:首次打开时,显示WelcomeActivity,1.5秒后跳转到登录界面MainActivity进行登录,保存登录信息,登录成功后跳转到StudentMainActivity。退出后再次打开程序,还是会先进入到WelcomeActivity,然后判断是否已经存在登录信息,存在的话直接模拟登录跳转到StudentMainActivity,否则跳转到MainActivity进行登录。

WelcomeActivity代码:

package com.example.edm;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.example.edm.JWUtils.ConnectJWGL;

import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;

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

    public void init() {
        sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);
        if(sharedPreferences.getString("id", null) == null) {
            ToastUtil.showMessage(this,"请先进行登录!");
            final Intent intent=new Intent(WelcomeActivity.this,MainActivity.class);
            Timer timer=new Timer();
            TimerTask timerTask=new TimerTask() {
                @Override
                public void run() {
                    startActivity(intent);
                }
            };
            timer.schedule(timerTask,1500);
        }else {
            try {
                Timer timer=new Timer();
                TimerTask timerTask=new TimerTask() {
                    @Override
                    public void run() {
                        String _id = sharedPreferences.getString("id", null);
                        String password = sharedPreferences.getString("in_net", null);
                        String password1 = sharedPreferences.getString("jw_password", null);
                        try {
                            //开始登录,这段自行发挥
                            MainActivity.connectJWGL = new ConnectJWGL(_id, password, password1, WelcomeActivity.this);
                            MainActivity.connectJWGL.init();
                            MainActivity.connectJWGL.beginLogin();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        final Intent intent=new Intent(WelcomeActivity.this,StudentMainActivity.class);
                        startActivity(intent);
                    }
                };
                timer.schedule(timerTask,1500);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
sharedPreferences.getString("id", null) == null

表示还未进行过登录,跳转到登录界面。

登录界面MainActivity:

sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String _id=textId.getText().toString();
String password=textPassword.getText().toString();
String password1 = textPassword1.getText().toString();
editor.putString("id", _id);
editor.putString("in_net", password1);
editor.putString("jw_password", password);
editor.commit();

获取了输入的账号密码之后,我们利用SharedPreferences将账号保存下来即可。

猜你喜欢

转载自blog.csdn.net/Cyril_KI/article/details/108477334
今日推荐