package com.cj.autojs.dtok; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.google.gson.Gson; import com.cj.autojs.dtok.entity.User; import com.cj.autojs.dtok.util.SharedPreferencesUtils; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; public class SplashActivity extends BaseActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.splash_screen); textView = findViewById(R.id.app_version); textView.setText(getVersionName()); new Handler().postDelayed(new Runnable() { @Override public void run() { checkLogin(); } }, 2000); } public void checkLogin() { Intent toLogin = new Intent(this, LoginActivity.class); Intent toMain = new Intent(this, MainActivity.class); if (SharedPreferencesUtils.contains(this, "token")) { Api.INSTANCE.setToken(SharedPreferencesUtils.getString(this, "token", "")); new Thread(new Runnable() { @Override public void run() { Api.INSTANCE.checkLogin(new Callback() { @Override public void onFailure(Call call, IOException e) { SplashActivity.this.runOnUiThread(() -> { Toast.makeText(SplashActivity.this, "登录异常", Toast.LENGTH_SHORT).show(); }); startActivity(toLogin); finish(); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.body() != null) { try { JSONObject object = new JSONObject(response.body().string()); if (object.getBoolean("success")) { Gson gson = new Gson(); User user = gson.fromJson(object.getJSONObject("data").toString(), User.class); SharedPreferencesUtils.saveUserInfo(SplashActivity.this, user); startActivity(toMain); finish(); return; } } catch (JSONException e) { throw new RuntimeException(e); } } startActivity(toLogin); finish(); } }); } }).start(); }else{ startActivity(toLogin); finish(); } } }