【问题标题】:progress dialog is not working while calling new MyAsyncTask().execute().get() Android调用 new MyAsyncTask().execute().get() Android 时进度对话框不起作用
【发布时间】:2014-06-18 09:55:42
【问题描述】:

如何在下面的 AsyncTask android 的 onPreExecute() 上显示进度对话框是我的代码 在此 onPreExecute() 方法进度对话框中,UI 线程上未显示

//调用AsyncTask

try {

        new XYZ().execute(employeeId, password).get();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


     class XYZ extends AsyncTask<String, Integer, Void> {

    ProgressDialog simpleWaitDialog;

    @Override
    protected void onPreExecute() {


        simpleWaitDialog =  new ProgressDialog(TreeViewListDemo.this);
        System.out.println("fgfdgfgfgf"+simpleWaitDialog);
        simpleWaitDialog.setMessage("Loading...");
        simpleWaitDialog.setIndeterminate(true);
        simpleWaitDialog.setCancelable(true);


        simpleWaitDialog.show();

    }

    @SuppressWarnings("unchecked")
    @Override
    protected Void doInBackground(String... params) {

        String username = params[0];
        String password = params[1];
        // this is code for perfoming SOAP web services which is working    fine and getting data from server side
        return null;
    }

}

【问题讨论】:

  • 不要做new XYZ().execute(employeeId, password).get();。
  • 这实际上是我的问题。请问我该怎么问这个问题。@PankajKumar

标签: android android-intent android-activity android-asynctask


【解决方案1】:

您正在调用get(),大概来自 UI 线程。从文档中,get() 方法:

如有必要,等待计算完成,然后检索 结果。

这意味着您阻塞了 UI 线程,使其无法显示进度对话框。我想你想用execute()替换对get()的调用

【讨论】:

  • @AmarRaj 我已经编辑建议您应该调用的方法
  • 没有get()我没有得到服务器数据,所以我使用get()方法和execute()方法
  • 是什么让你认为你没有得到数据?您在代码中的评论说网络部分正在工作......
【解决方案2】:

进度对话框显示直到 doInBackground 方法被执行。

在这里你只是在两行以下运行

String username = params[0];
String password = params[1];

几乎不需要以毫秒为单位的时间,因此您的进度会在毫秒内显示并关闭。 请将日志值放入 onPreExecute() 以检查您的进度对话框,显示我们没有。

AsyncTask is normally used to perform Network operations..!!

Happy 编码..!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    相关资源
    最近更新 更多