猿问

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 是空白的?编辑:不阻塞用户界面


牧羊人nacy
浏览 188回答 3
3回答

墨色风雨

AsyncTask 用于执行后台操作并在 UI 线程上发布结果。在您的情况下,您应该放入eins.setTextAsyncTask onPostExecute。另一个问题是因为 AsyncTask 是一个单独的类,所以您需要定义一个接口将结果传递回 MainActivity。消息public class News extends AsyncTask<String, Void, String> {&nbsp; &nbsp; private WeakReference<OnNewsListener> mOnNewsListener;&nbsp; &nbsp; public void setOnNewsListener(OnNewsListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; mOnNewsListener = new WeakReference<>(listener);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected String doInBackground(String... strings) {&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder builder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder builder2 = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String title = doc.title();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Elements links = doc.select("h2");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Elements links2 = doc.select("h3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(title).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Element link : links) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(link.text()).append("$");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Element link : links2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder2.append(link.text()).append("$");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String text = builder.toString() + "%" + builder2.toString();&nbsp; &nbsp; &nbsp; &nbsp; return text;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPostExecute(String text) {&nbsp; &nbsp; &nbsp; &nbsp; if (mOnNewsListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mOnNewsListener.get() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mOnNewsListener.get().onNews(text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public interface OnNewsListener {&nbsp; &nbsp; &nbsp; &nbsp; void onNews(String text);&nbsp; &nbsp; }}主要活动public class MainActivity extends AppCompatActivity implements News.OnNewsListener{&nbsp; &nbsp; TextView eins;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; eins = findViewById(R.id.eins);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; &nbsp; &nbsp; News news = new News();&nbsp; &nbsp; &nbsp; &nbsp; news.setOnNewsListener(this);&nbsp; &nbsp; &nbsp; &nbsp; news.execute();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onNews(String text) {&nbsp; &nbsp; &nbsp; &nbsp; eins.setText(text);&nbsp; &nbsp; }}

ibeautiful

根据pz64的建议,在方法中设置文本onPostExecute()并调用AsyncTask()不调用get()方法。get()AsyncTask 上的方法使任务同步并影响您的 UI。public class News extends AsyncTask<String, Void, String> {&nbsp; &nbsp; @override&nbsp; &nbsp; protected void onPreExecute(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//initiate your loading views&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected String doInBackground(String... strings) {&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder builder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder builder2 = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String title = doc.title();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Elements links = doc.select("h2");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Elements links2 = doc.select("h3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(title).append("\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Element link : links) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(link.text()).append("$");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Element link : links2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder2.append(link.text()).append("$");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String text = builder.toString() + "%" + builder2.toString();&nbsp; &nbsp; &nbsp; &nbsp; return text;&nbsp; &nbsp; }&nbsp; &nbsp; @override&nbsp; &nbsp; protected void onPostExecute(String response){&nbsp; &nbsp; &nbsp; &nbsp; //dispose loading views&nbsp; &nbsp; &nbsp; &nbsp; if(response != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eins.setText(response);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//could not load&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}称呼:@Overrideprotected void onResume(){&nbsp; &nbsp; super.onResume();&nbsp; &nbsp; new News().execute(); //do not call get method}

忽然笑

您可以在 oncreate 方法中调用 asyncTask。并在 onProgressUpdate 方法中设置结果。@Overrideprotected void onProgressUpdate(String... text) {&nbsp; &nbsp; eins.setText.setText(text);&nbsp; &nbsp;&nbsp;}
随时随地看视频慕课网APP

相关分类

Java
我要回答