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();
    }
相關文章
相關標籤/搜索