开发者社区> 问答> 正文

运行postDelayed处理程序时冻结AsynchTask

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

我有一个初始屏幕,用于加载应用程序需要运行的各种功能(配置文件,来自Internet的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函数就可以工作。所以我认为这与此有关。

展开
收起
Puppet 2020-01-06 22:27:35 597 0
1 条回答
写回答
取消 提交回答
  • 我认为这是可行的:

    
        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);
            }
    
    }
    
    2020-01-06 22:27:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载