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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值