??專案需求:每次退出程式再次啟動時都需要重新進行登錄,現在需要仿照微信,實作每次啟動后自動登錄,
??專案用到的三個界面:WelcomeActivity(歡迎界面),MainActivity(登錄界面),StudentMainActivity(真正的主界面),需要實作的效果是:首次打開時,顯示WelcomeActivity,1.5秒后跳轉到登錄界面MainActivity進行登錄,保存登錄資訊,登錄成功后跳轉到StudentMainActivity,退出后再次打開程式,還是會先進入到WelcomeActivity,然后判斷是否已經存在登錄資訊,存在的話直接模擬登錄跳轉到StudentMainActivity,否則跳轉到MainActivity進行登錄,
WelcomeActivity代碼:
package com.example.edm;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.example.edm.JWUtils.ConnectJWGL;
import java.util.Timer;
import java.util.TimerTask;
public class WelcomeActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
init();
}
public void init() {
sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);
if(sharedPreferences.getString("id", null) == null) {
ToastUtil.showMessage(this,"請先進行登錄!");
final Intent intent=new Intent(WelcomeActivity.this,MainActivity.class);
Timer timer=new Timer();
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
startActivity(intent);
}
};
timer.schedule(timerTask,1500);
}else {
try {
Timer timer=new Timer();
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
String _id = sharedPreferences.getString("id", null);
String password = sharedPreferences.getString("in_net", null);
String password1 = sharedPreferences.getString("jw_password", null);
try {
//開始登錄,這段自行發揮
MainActivity.connectJWGL = new ConnectJWGL(_id, password, password1, WelcomeActivity.this);
MainActivity.connectJWGL.init();
MainActivity.connectJWGL.beginLogin();
} catch (Exception e) {
e.printStackTrace();
}
final Intent intent=new Intent(WelcomeActivity.this,StudentMainActivity.class);
startActivity(intent);
}
};
timer.schedule(timerTask,1500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
sharedPreferences.getString("id", null) == null
表示還未進行過登錄,跳轉到登錄界面,
登錄界面MainActivity:
sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String _id=textId.getText().toString();
String password=textPassword.getText().toString();
String password1 = textPassword1.getText().toString();
editor.putString("id", _id);
editor.putString("in_net", password1);
editor.putString("jw_password", password);
editor.commit();
獲取了輸入的賬號密碼之后,我們利用SharedPreferences將賬號保存下來即可,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/4437.html
標籤:其他
上一篇:ApiCloud入門之旅-(表白神器app的完整開發流程)
下一篇:Vue 的axios的使用
