SplashScreen这是正确的方法吗



请注意,以下似乎可以正常工作。我只需要意见,如果这是正确的逻辑,或者如果我能做任何更好的

这是我第一次使用闪屏。到目前为止(或者我认为是这样(,如果我使用的逻辑是好的,或者我可以改进它,我只需要一些意见。因此,基本上在后台,我有一个名为Reader(异步任务(的类,它从7个不同的链接中读取(正如您将看到的那样,它被调用了7次(并填充我的数据库。我想让splashscreen填充数据库,然后运行mainActivity。(也有足够长的屏幕显示赞助商(如果有的话(

所以我想出了这个。我知道我不能让我的异步任务是非静态的,但我不能改变变量。(isAsyncCompleted(从此站点看到了此方法。

public class SplashActicity extends AppCompatActivity {
boolean isHandlerCompleted, isAsyncCompleted = false;
private static int SPLASH_SCREEN_TIME_OUT = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_acticity);
MyTask myTask = new MyTask();
myTask.execute();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
isHandlerCompleted = true;
if (isHandlerCompleted && isAsyncCompleted) {
Intent intent = new Intent(SplashActicity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
}, SPLASH_SCREEN_TIME_OUT);

}
private class MyTask extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids) {
Reader fromthepast = new Reader(getApplicationContext(), "http://www.lemesosblog.com/index.php?option=com_obrss&task=feed&id=7%3Alemesos-apo-to-parelthon&format=feed", "fromthepast");
Reader international = new Reader(getApplicationContext(), "http://www.lemesosblog.com/index.php?option=com_obrss&task=feed&id=9%3Alemesos-diethni&format=feed", "international");
Reader latestNews = new Reader(getApplicationContext(), "http://lemesosblog.com/index.php?option=com_obrss&task=feed&id=3%253Alemesos-teleftea-nea&format=feed&fbclid=IwAR1VeGagGZD_M_ACBx8tAA38afhVallFc5LG6U58HYCq8iLJFNLKsaXtVAI", "latestNews");
Reader health = new Reader(getApplicationContext(), "http://www.lemesosblog.com/index.php?option=com_obrss&task=feed&id=6:lemesos-ygeia&format=feed", "health");
Reader technology = new Reader(getApplicationContext(), "http://www.lemesosblog.com/index.php?option=com_obrss&task=feed&id=5%3Alemesos-tech&format=feed", "technology");
Reader economy = new Reader(getApplicationContext(), "http://www.lemesosblog.com/index.php?option=com_obrss&task=feed&id=4:lemesos-oikonomia&format=feed", "economy");
Reader tepak = new Reader(getApplicationContext(), "http://www.lemesosblog.com/index.php?option=com_obrss&task=feed&id=8:lemesos-tepak&format=feed", "tepak");
economy.execute();
health.execute();
fromthepast.execute();
international.execute();
latestNews.execute();
technology.execute();
tepak.execute();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
isAsyncCompleted = true;
super.onPostExecute(aVoid);
}
}

}

在您的情况下,如果处理程序在异步完成之前完成了执行,那么您的条件将无效,您的代码将不会执行。如果没有任何特定的要求阻止下一个活动直到指定的超时,您应该将代码从handler移动到onPostExecute()

相关内容

最新更新