首页 > 解决方案 > Android Studio Activity 启动延迟

问题描述

我有一个异步任务:

 package e.marco.swimcommit;


import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;


public class News extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... strings) {
        final StringBuilder builder = new StringBuilder();
        final StringBuilder builder2 = new StringBuilder();
        {
            try {
                Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();
                String title = doc.title();
                Elements links = doc.select("h2");
                Elements links2 = doc.select("h3");
                builder.append(title).append("\n");
                for (Element link : links) {
                    builder.append(link.text()).append("$");
                }
                for (Element link : links2) {
                    builder2.append(link.text()).append("$");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String text = builder.toString() + "%" + builder2.toString();
        return text;
    }
}

和我的 MainActivity 中的 onResume Methode 将返回的文本设置在文本视图中

  @Override
    protected void onResume()
    {
        super.onResume();
        try {
            eins.setText(new News().execute().get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

但是,如果我启动应用程序,它会显示一个白屏,直到 onResume Methode 获取文本并将其设置为 Textview。如何在没有延迟启动的情况下加载应用程序显示所有其他元素,如按钮背景等?以便在 onResume Methode 获取信息并设置之前 Textview 是空白的?编辑:不阻塞用户界面

标签: javaandroidperformance

解决方案


AsyncTask 用于执行后台操作并在 UI 线程上发布结果。在您的情况下,您应该放入eins.setTextAsyncTask onPostExecute

另一个问题是因为 AsyncTask 是一个单独的类,所以您需要定义一个接口将结果传递回 MainActivity。

消息

public class News extends AsyncTask<String, Void, String> {
    private WeakReference<OnNewsListener> mOnNewsListener;

    public void setOnNewsListener(OnNewsListener listener) {
        mOnNewsListener = new WeakReference<>(listener);
    }

    @Override
    protected String doInBackground(String... strings) {
        final StringBuilder builder = new StringBuilder();
        final StringBuilder builder2 = new StringBuilder();
        {
            try {
                Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();
                String title = doc.title();
                Elements links = doc.select("h2");
                Elements links2 = doc.select("h3");
                builder.append(title).append("\n");
                for (Element link : links) {
                    builder.append(link.text()).append("$");
                }
                for (Element link : links2) {
                    builder2.append(link.text()).append("$");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String text = builder.toString() + "%" + builder2.toString();
        return text;
    }

    @Override
    protected void onPostExecute(String text) {
        if (mOnNewsListener != null) {
            if (mOnNewsListener.get() != null) {
                mOnNewsListener.get().onNews(text);
            }
        }
    }

    public interface OnNewsListener {
        void onNews(String text);
    }
}

主要活动

public class MainActivity extends AppCompatActivity implements News.OnNewsListener{
    TextView eins;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        eins = findViewById(R.id.eins);
    }

    @Override
    protected void onResume() {
        super.onResume();

        News news = new News();
        news.setOnNewsListener(this);
        news.execute();
    }

    @Override
    public void onNews(String text) {
        eins.setText(text);
    }
}

推荐阅读