请注意以下内容似乎正常。如果这是正确的逻辑,或者如果我能做得更好的事情,我只需要意见
所以这是我第一次使用启动屏幕。到目前为止(或我认为)可以按预期工作,如果我使用的逻辑很好或者是否可以改善此情况,我仅需要一些意见。所以基本上在后台,我有一个叫做Reader(异步任务)的类,它从7个不同的链接中读取(您将看到它被调用7次) 并填充我的数据库。我希望我的启动画面要做的是填充该数据库,然后运行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);
}
}
}
答案 0 :(得分:0)
在您的情况下,如果处理程序在异步完成之前完成了执行,则您的条件将无效并且您的代码将不会被执行。如果没有特定的要求阻止下一个活动直到指定的超时,则应将handler
中的代码移至onPostExecute()