【问题标题】:AsynchTask freezing while a postDelayed handler is running当 postDelayed 处理程序运行时,AsynchTask 冻结
【发布时间】:2020-01-06 10:10:24
【问题描述】:

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

我有一个启动画面,用于加载应用程序需要运行的各种内容(配置文件、来自 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 函数就会起作用。所以我认为这与此有关。

【问题讨论】:

    标签: java android android-studio android-asynctask


    【解决方案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);
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-23
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 2021-12-04
      • 2016-06-27
      • 2016-05-01
      • 2014-02-21
      • 2021-01-22
      相关资源
      最近更新 更多