java - 当 postDelayed 处理程序运行时,AsynchTask 卡住

标签 java android android-studio android-asynctask

我对应用程序开发还很陌生,但我有一个无法解决的问题。

我有一个启动屏幕,我用它来加载应用程序运行所需的各种内容(配置文件、来自互联网的 html),而后者给我带来了一个巨大的问题。这是代码

Document doc = null;

protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splash);

        //Fix loading data, dunno why this happens
        try {
            doc = new getHtml().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        /* New Handler to start the Menu-Activity
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Utils.saveData(Splash.this, doc);
                Intent mainIntent = new Intent(Splash.this, PocketFrameNavigation.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

public static class getHtml extends AsyncTask<Void, Void, Document> {

        protected Document doInBackground(Void... args) {
            try {
                return Jsoup.connect(DROP_DATA_URL).get();
            } catch(Exception e) {
                return null;
            }
        }

        protected void onPostExecute(Document html){
            doc = html;
        }
    }

给我带来问题的代码是 try 语句内部。无论我把它放在哪里,它似乎都会卡住主线程。我做错了什么吗?预先感谢您的帮助。

此外,只要不涉及后期延迟处理程序,getHTML 函数就会起作用。所以我认为这与此有关。

最佳答案

我认为这会起作用:

    Document doc = null;

        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.splash);

            new GetHTMLContent().excute();


        }

        private static class GetHTMLContent extends AsyncTask<Void, Void, Document> {

            protected Document doInBackground(Void... args) {
                try {
                    return Jsoup.connect(DROP_DATA_URL).get();
                } catch(Exception e) {
                    return null;
                }
            }

            protected void onPostExecute(Document html){
                doc = html;
                Utils.saveData(Splash.this, doc);
                goToMainActivity();


            }
        }

        private void goToMainActivity(){
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {
                    /* Create an Intent that will start the Menu-Activity. */
                    Intent mainIntent = new Intent(Splash.this, PocketFrameNavigation.class);
                    Splash.this.startActivity(mainIntent);
                    Splash.this.finish();
                }
            }, SPLASH_DISPLAY_LENGTH);
        }

}

关于java - 当 postDelayed 处理程序运行时,AsynchTask 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610160/

相关文章:

java - 如何在具有工厂方法的 spring MVC Controller 中进行集成测试?

java - 将 JRadioButton 添加到图形对象

Java XML 解析器添加不必要的 xmlns 和 xml :space attributes

android - 使用 NativeActivity 在 android NDK 中进行键盘输入

java - 使用 Test Fairy 插件上传 APK 问题

java - 如何在按钮中容纳长文本?

java - 我可以知道我的库何时从 Java 或 Kotlin 调用吗?

android - 撰写中的文本字段 : Double Tap to select text is not working

java - Bottom Sheet Android-滚动问题

Android登录注册中java.lang.String无法转换为JSONObject