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

因此,这是我第一次使用启动屏幕。到目前为止(或我认为)可以按预期工作,如果我使用的逻辑很好或者是否可以改善此情况,我仅需要一些意见。所以基本上在后台,我有一个叫做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);
    }
}


}

最佳答案

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

关于android - SplashScreen这是正确的方法吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53609798/

10-13 04:23