Android Studio活动启动延迟

时间:2019-03-01 07:20:03

标签: java android performance

我有一个AsyncTask:

 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方法,用于在textview中设置返回的文本

  @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。如何在不延迟启动的情况下加载显示所有其他元素(如按钮背景等)的App?以便在onResume方法获取信息并进行设置之前,Textview是空白的? 编辑:不阻止用户界面

3 个答案:

答案 0 :(得分:2)

AsyncTask用于执行后台操作并在UI线程上发布结果。您应将eins.setText放入AsyncTask的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);
    }
}

MainActivity

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);
    }
}

答案 1 :(得分:0)

根据pz64的建议,在onPostExecute()方法中设置文本并在不调用AsyncTask()方法的情况下调用get()。 AsyncTask上的get()方法使任务同步,并且还会影响您的UI。

public class News extends AsyncTask<String, Void, String> {

    @override
    protected void onPreExecute(){
         //initiate your loading views
    }

    @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 response){
        //dispose loading views
        if(response != null){
            eins.setText(response);
        }else{
             //could not load
        }
    }
}

致电:

@Override
protected void onResume()
{
    super.onResume();
    new News().execute(); //do not call get method
}

答案 2 :(得分:0)

您可以在oncreate方法中调用asyncTask。 并在onProgressUpdate方法中设置结果。

@Override
protected void onProgressUpdate(String... text) {
    eins.setText.setText(text);    
}