如何防止ProgressBar粘连?

时间:2018-05-31 12:31:51

标签: android progress-bar ion

在我的应用中,我通过离子库从网站获得了一些TEXT,我使用progressBar来显示下载该数据的进度。

但是我的主要问题是在onComplete我实现了一些与我从网站上获得的TEXT一起使用的方法,这些是一些循环方法,它们使用了很多东西和其他东西,实际上我的progressBar在开始时工作正常,但是当它完整下载数据时,progressBar只会坚持到onComplete()完成所有方法。

我认为progressBar无论如何都会运行,如果不从onComplete()移除方法就不会卡住这种可能吗?

这是我使用我在onClick()事件中调用的离子库的函数:

private void getHTML(){
    progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
    progressDialog.show();
    Ion.with(getApplicationContext())
            .load("IP")
            .asString()
            .setCallback(new FutureCallback<String>() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onCompleted(Exception e, String result) {
                    htmlresultart = result;
                    htmlresultart = htmlresultart.replace("</td>", "\n");
                    getTable();
                    getHeader();
                    getBody();
                    progressDialog.cancel();
                }
            });

}

1 个答案:

答案 0 :(得分:1)

如果onCompleted()回调中调用的方法执行过多,则需要在后台线程上运行。在这种情况下,使用Ion进行回调是没有意义的,相反,您应该同步获取数据,然后执行所有其他任务,所有这些都在一个后台线程上,如下所示:

private void getHTML(){
    progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
    progressDialog.show();
    // start a background thread
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() {
         @Override
         public void run() {
            String htmlresultart = null;
            try {  
                 String htmlresultart = Ion.with(getApplicationContext())
                   .load("IP")
                   .asString()
                   .get(); 
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            // if htmlresultart is null at this point, an exception occured and the string couldn't be fetched so you most likely should abort the following processing.
            if (htmlresultart != null) {
                htmlresultart = htmlresultart.replace("</td>", "\n");
                getTable();
                getHeader();
                getBody();
                progressDialog.cancel();
            }
         }
    });        
}
相关问题