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

1.在AndroidMainfest.xml文件中设置android

<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方法ide

@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.在登陆界面 LoginActivitythis

登陆按钮的方法中:spa

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();
    }
相关文章
相关标签/搜索