我的启动画面应该可见3秒钟,而在后台应用程序中则检查用户是否已登录。但是,检查速度如此之快,以至于我的启动画面只是闪烁并开始活动。这是代码。如何使后台任务等待启动时间完成? tnx!这是代码:
public class SplashActivity extends AppCompatActivity {
ImageView splash;
private static final int SPLASH_SHOW_TIME = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
splash = findViewById(R.id.splash);
splash.bringToFront();
new taskCheckLogin().execute();
}
private class taskCheckLogin extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(SplashActivity.this);
boolean isLoggedIn = prefs.getBoolean("isLoggedIn", false);
prefs.edit().putBoolean("isLoggedIn", isLoggedIn).apply();
if (isLoggedIn) {
Intent i = new Intent(SplashActivity.this, OCR.class);
startActivityForResult(i, 1);
} else {
Intent p = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(p);
}
try {
Thread.sleep(SPLASH_SHOW_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
}