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();
}