Android 应用 记住登录状态,二次自动登录

1.在AndroidMainfest.xml文件中设置

<activity android:name=".TabBarActivity">
    <intent-filter> //
        <action android:name="android.intent.action.MAIN" />// 告诉安卓系统是主界面
        <category android:name="android.intent.category.LAUNCHER" /> //把应用显示在程序列表里
    </intent-filter>
</activity>

2.在TabBarActivity  onCreate方法

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabbar_activity);
    initListView();
    //去取登录的token,在第一运行应用时,token是空,
    SharedPreferences sp = getSharedPreferences("token", Context.MODE_PRIVATE);
    String token = sp.getString("token","");
    if (token.isEmpty() || token == null){    
        //为空时会直接跳转到登录界面
        this.startActivity(new Intent(this.getApplicationContext(),LoginActivity.class));
        finish();
    }else {
        //实现token登录请求
        System.out.println("二次登录验证O(∩_∩)O哈哈~");
    }
    bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
    refresh();
}

3.在登录界面 LoginActivity

登录按钮的方法中:

public void clickLoginActionHandle(View source){

        // 登录请求 在成功的回调中将返回的token数据用SharedPreferences将token持久化 并 跳转到登录成功的界面
        SharedPreferences sp = getSharedPreferences("token",Context.MODE_PRIVATE); 
        SharedPreferences.Editor editor = sp.edit(); 
        String token = "111111111111"; 
        editor.putString("token",token); 
        editor.commit();

        Intent intent = new Intent();
        intent.setClass(LoginActivity.this,TabBarActivity.class);
        startActivity(intent);
        finish();
    }

 

转载于:https://my.oschina.net/u/2532095/blog/864908

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现 Android 记住密码和自动登录的简单代码示例: 1. 记住密码: 在登录页面的登录按钮点击事件中,添加如下代码: ```java // 获取账号和密码的EditText EditText etAccount = findViewById(R.id.et_account); EditText etPassword = findViewById(R.id.et_password); // 获取记住密码的CheckBox CheckBox cbRememberPwd = findViewById(R.id.cb_remember_pwd); if (cbRememberPwd.isChecked()) { // 如果勾选了记住密码,则将账号和密码保存到SharedPreferences中 SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("account", etAccount.getText().toString()); editor.putString("password", etPassword.getText().toString()); editor.putBoolean("remember_pwd", true); editor.apply(); } else { // 如果没有勾选记住密码,则清空SharedPreferences中的账号和密码 SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove("account"); editor.remove("password"); editor.putBoolean("remember_pwd", false); editor.apply(); } ``` 2. 自动登录: 在应用启动的时候,在 onCreate() 方法中添加如下代码: ```java // 获取保存的账号和密码 SharedPreferences sp = getSharedPreferences("login_info", MODE_PRIVATE); String account = sp.getString("account", ""); String password = sp.getString("password", ""); boolean rememberPwd = sp.getBoolean("remember_pwd", false); if (rememberPwd) { // 如果勾选了记住密码,则填充账号和密码,并且自动登录 EditText etAccount = findViewById(R.id.et_account); EditText etPassword = findViewById(R.id.et_password); etAccount.setText(account); etPassword.setText(password); // 在这里添加登录操作,即可实现自动登录 } ``` 注意:在自动登录的时候,需要先检查账号和密码是否为空,如果为空,则不能自动登录。同时,为了保障账号和密码的安全,需要对密码进行加密存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值