SplashActivity.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.cj.autojs.dtok;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.Window;
  6. import android.view.WindowManager;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import com.google.gson.Gson;
  11. import com.cj.autojs.dtok.entity.User;
  12. import com.cj.autojs.dtok.util.SharedPreferencesUtils;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15. import java.io.IOException;
  16. import okhttp3.Call;
  17. import okhttp3.Callback;
  18. import okhttp3.Response;
  19. public class SplashActivity extends BaseActivity {
  20. TextView textView;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. requestWindowFeature(Window.FEATURE_NO_TITLE);
  25. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  26. setContentView(R.layout.splash_screen);
  27. textView = findViewById(R.id.app_version);
  28. textView.setText(getVersionName());
  29. new Handler().postDelayed(new Runnable() {
  30. @Override
  31. public void run() {
  32. checkLogin();
  33. }
  34. }, 2000);
  35. }
  36. public void checkLogin() {
  37. Intent toLogin = new Intent(this, LoginActivity.class);
  38. Intent toMain = new Intent(this, MainActivity.class);
  39. if (SharedPreferencesUtils.contains(this, "token")) {
  40. Api.INSTANCE.setToken(SharedPreferencesUtils.getString(this, "token", ""));
  41. new Thread(new Runnable() {
  42. @Override
  43. public void run() {
  44. Api.INSTANCE.checkLogin(new Callback() {
  45. @Override
  46. public void onFailure(Call call, IOException e) {
  47. SplashActivity.this.runOnUiThread(() -> {
  48. Toast.makeText(SplashActivity.this, "登录异常", Toast.LENGTH_SHORT).show();
  49. });
  50. startActivity(toLogin);
  51. finish();
  52. }
  53. @Override
  54. public void onResponse(Call call, Response response) throws IOException {
  55. if (response.body() != null) {
  56. try {
  57. JSONObject object = new JSONObject(response.body().string());
  58. if (object.getBoolean("success")) {
  59. Gson gson = new Gson();
  60. User user = gson.fromJson(object.getJSONObject("data").toString(), User.class);
  61. SharedPreferencesUtils.saveUserInfo(SplashActivity.this, user);
  62. startActivity(toMain);
  63. finish();
  64. return;
  65. }
  66. } catch (JSONException e) {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. startActivity(toLogin);
  71. finish();
  72. }
  73. });
  74. }
  75. }).start();
  76. }else{
  77. startActivity(toLogin);
  78. finish();
  79. }
  80. }
  81. }